Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to write .htacess file that would catch urls like:

http://www.site.com/pageone.html

http://www.site.com/pagetwo.html

and redirect them to index.php so it would receive 'pageone' and 'pagetwo' as GET parameters.

Below is my htacess:

Options +FollowSymlinks

AuthUserFile /dev/null

AuthGroupFile /dev/null

RewriteEngine On

RewriteOptions inherit

RewriteRule ^((?:.[^/])+).html$ index.php?page=$1 [L]

AddHandler php5-script .php

The problem with this regexp is that it catches some pages while showing 404 error on others. And i don't understand this behavior.

For example

http://www.site.com/draw.html

works fine. I get 'draw' as GET parameter in my script.

But

http://www.site.com/draws.html

redirects to 404 page

Could anyone explain what is wrong with my htacess ?

p.s. - i am seeing this on a local Apache server under typical MAMP config.

share|improve this question

1 Answer 1

up vote 2 down vote accepted
RewriteRule ^((?:.[^/])+).html$ index.php?page=$1 [L]
  • ^ = anchor at the start of the URI
  • (?: = extended expression to group without creating a backreference; unsure what the purpose is here.
  • . = match a single character.
  • [^/] = match anything that's not a slash
  • + = all of the above, repeated one or more times
  • .html = must end in .html
  • $ = anchor the end of the URI.

This code would match, among other things, /ab/cd/ef/foo.html, which will never occur in a per-directory context.
It would not match odd-length resources or directories, since you explicitly ignore those with (.[^/]).

Since you don't specify if multiple subdirectories should be handled or not, just matching what you described can be done much, much easier with:

RewriteRule ^([^/]+).html$ index.php?page=$1 [L]

For further help with troubleshooting, see the RewriteLog documentation

share|improve this answer
    
Thanks, it works. And your regexp looks much more elegant. But the question remains - why my regexp is not working ? In fact it won't match any '/ab/cd/ef/foo.html' urls as you have written. –  Termos Jan 15 '13 at 9:10
1  
I would not count blindly on extended PCRE working in mod_rewrite; I have not seen that used in years of apache support. –  adaptr Jan 15 '13 at 9:19
1  
He covered that too. Because your grouping matches two characters, it can only match even-length URLs. Furthermore, every second character cannot be a slash so /a/b/c.html will match and /ab/c/d.html won't. Lastly, the leading slash is removed in .htaccess files which reverses the meaning of the previous sentence. –  Ladadadada Jan 15 '13 at 9:41
    
Not reverse, but if the first character is a slash it will never match, regardless of the rest of the URI. –  adaptr Jan 15 '13 at 10:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.