1

I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]

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

RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2

RewriteRule ^php/$ error/

Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).

Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Higgsy
  • 324
  • 1
  • 14

2 Answers2

1

RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).

Your file could be similar to this:

RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]

You may be interested in the Apache Mod_Rewrite Documentation.

Scott Stevens
  • 2,546
  • 1
  • 20
  • 29
  • This worked! Thanks! But, the bottom two `RewriteRule`s didn't work. I had to change the `[^/]` to `[a-zA-Z0-9]`. It all works now. Thanks! – Higgsy Jun 10 '12 at 01:45
  • Ok, that's odd. Unless you only want the letters and numbers, [^/] matches everything except /, so was it refusing to redirect, or was it just not your intended behaviour? I also just added /? to the bottom regex, since I somehow left it off. (Certain mobile devices don't have a tilde key, sorry!) – Scott Stevens Jun 10 '12 at 02:37
0
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

to either not execute the rule on a directory or a file

More info on the mod_rewrite documentation pages, search for CondPattern

Harald Brinkhof
  • 4,375
  • 1
  • 22
  • 32