1

Ok,

so I am using (or trying to use) two primary mod_rewrite rules, and they seem to be conflicting with one another

RewriteRule ^/?help$ index.php?page=help [L]

and

RewriteRule ^/?([a-zA-Z0-9._-]+)$ index.php?user=$1 [L]

If I get rid of the period ->. in the second rule, my help page is displayed, and I can display a user page as well, but when I add the period, my help page doesn't display, but instead (I think) gets processed as a user page.

Anyone have any pointers?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

2 Answers2

0

First, if you are doing this in .htaccess mod_rewrite does not stop processing but instead does an internal redirect. See this answer for details.

You need to escape the period in your second rewrite rule. Period (or dot) will match any character if not escaped.

Fixed rule:

RewriteRule ^/([a-zA-Z0-9\._-]+)$ /index.php?user=$1 [L]

Edit 3:

Based on your comments and own answer, here's an updated solution. The way RewriteCond %{REQUEST_FILENAME} works in Apache depends on the context you are in. You can also omit the ? from the matching pattern.

RewriteRule ^/help$ /index.php?page=help [L]
# If you are inside <VirtualHost> context
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/([a-zA-Z0-9\._-]+)$ /index.php?user=$1 [L]
# If you are inside <Directory> or .htaccesscontext
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([a-zA-Z0-9\._-]+)$ /index.php?user=$1 [L]

Do bear in mind that this solution causes conflicts if there are users with the same name as any of the files or directories you have on your server.

Community
  • 1
  • 1
Ketola
  • 2,767
  • 18
  • 21
  • I am trying to make ?page=help direct to /help, and ?user=user.name direct to /user.name (I should have posted that originally...sorry.). Even escaping the period doesn't direct the help page properly. It's being read as a user page. When I echo the $_GET['user'] variable of the help page, it says "index.php". I tried the rewrite conditions also, but it gave me a 404. – Ben Althauser Dec 07 '12 at 21:08
  • Thanks for the response btw. – Ben Althauser Dec 07 '12 at 21:14
  • To clarify, my site url would be www.site.com/help, and php would process index.php?page=help internally. It works, but when I try to add a "user" rule, it doesn't unless I exclude the period. – Ben Althauser Dec 07 '12 at 21:17
  • Reverted back to my original answer based on your comments. Tested to work with periods. Furthermore if you are within context and not .htaccess, and you want to avoid matching existing files as your own answer suggests, you need to modify the RewriteCond a bit. – Ketola Dec 09 '12 at 10:55
0

I looked at something another user ran into here - Apache / mod_rewrite / Periods messing with pattern , and modified his rule, and it is working -

RewriteRule ^/?help$ index.php?page=help [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9\[\]\.()+|_-]+)$ index.php?user=$1 [L]

Thankyou for the response.

Community
  • 1
  • 1