Performance issue with some Mod_rewrite code

I’m new to mod_rewrite. I have written a page of code and have not experiences any performance issues until now. The following code causes my pages to take several seconds longer to load regardless of where I move it to among the code and whether or not the code actually gets used (unless its getting used and I’m not realizing it). It adds slashes after a webpage and I’m using it to standardize everything after some site wide changes are being made.

RewriteCond %{REQUEST_URI} !city1$
RewriteCond %{REQUEST_URI} !city2$
RewriteCond %{REQUEST_URI} !(.)/$
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.
)$$1/ [R=301,L]



First, WELCOME to SitePoint’s Apache forum!

Now, WHY in the world would you ever want to add a / after the filename? Options MultiViews? IMHO, that’s one of the WORST things that you can do to a URI!

Okay, to answer your question, ANYTHING you do via mod_rewrite requires the server to load the regex engine, read the .htaccess file (that’s why you should put mod-rewrite, after testing, of course, in the httpd.conf), parse all the mod_rewrite code and go through it repeatedly until no matches were found.

BECAUSE I can follow your code, wouldn’t it be easier to just say that you want all directories EXCEPT city1 and city2 to be followed by a /? This is NORMALLY handled by httpd.conf (I’m not sure of the code for that), all you’d need is:

RewriteEngine on
RewriteCond %{REQUEST_URI} !(city1|city2)$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

Here, city1 and city2 are excluded by the RewriteCond, files (including index.php) are excluded by the directory match condition and the trailing / is excluded by the NOT/ within the EVERYTHING atom.




Thank you for the reply. The reason I am using Options Multiviews is because I am hosting with godaddy (bad idea) and that will only work. There is nothing else that seems to work. The code is something that I picked up in another forum and it was suggested the best way to approach it. Your code is much cleaner though and I will be using it. Also, it was suggested to add slashes at the end of all my pages. Several pages with be in the form of /page/city? where the city will be a query and used for SEO and keeping it user friendly. To keep things standardized I was going to keep /page/ when there wasn’t a city however would you recommend removing the slash all the time?



On godaddy, changing file extensions will not work unless I use multiviews. Other users have experienced the same issue on linux shared servers. I will look into it more.

I have learned code from tutorials and online forums and found a concesus among code that seems to be widely used. I read over your forum and it helped a lot.

I am learning how to work with Apache and any suggestions to making it easier and improving speed are really appreciated. I have learned how to work with it and am now working on improving code. It makes sense to use leading slashes for directories and not to enforce them for file paths. I will read up more on Apache before I go ahead with any site changes.

Thanks for your input,


I am unaware that GoDaddy would twist anyone’s arm to USE MultiViews and would have trouble understanding why anyone would.

Nothing else will work, though, if you add a trailing / to force a file into a path. See why I think that forcing a trailing / (on anything other than a directory) is a BAD idea? Besides, what directory level is the visitor’s browser going to think it’s located? THAT is the root of the “Missing Support Files problem.”

If you pick-up junk in another forum, you should take problems with their code to them for resolution. All I can do is tell you what I believe should be used (when I get a reasonable “specification” for the code from the member).

THEN, anyone can suggest that you do something stupid with “all (your) pages” - my question is WHY WOULD YOU? Okay, that’s YOUR choice but I’ve recorded my advice here and in my first response.

/page/city? as in a query string or is that “city” a variable? If you want coding examples for using country/state/city with mod_rewrite, you should look at the development of regex code in my signature’s tutorial.

Removing the trailing slash all the time? Well, not to link to your domain’s DirectoryIndex (in DocumentRoot) as the browser will add that anyway. As for directories, yes, they SHOULD have the trailing /, too, as that will prevent Apache having to search for a file then decide you really wanted a directory THEN go fetch that directory’s DirectoryIndex. A Webmaster will learn to think like Apache and do things to make life easier for Apache so his/her pages will load faster. I call that “professionalism.”