Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top