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



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


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



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.

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

| 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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