homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Can someone please check my htaccess file?
A complex htaccess file with RewriteRules needs checking

 2:43 pm on Apr 23, 2010 (gmt 0)

Hi guys,

I've spent a lot of time making this htaccess file -- i've put it together with examples from the net, and some i worked out myself :-)

I'm just worried that because its been put together from loads of bits -- it could be slow, and not the best way to do certain things.

Anyway, thanks to anyone who can help me improve the file.


Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteRule ^.*$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^.*$ %{REQUEST_URI}/ [R=301,L]

RewriteRule ^([0-9a-zA-Z-]+)/?(([0-9a-zA-Z-]+)/)?$ /index.php?p=$1&l=$3 [L]



 4:02 pm on Apr 23, 2010 (gmt 0)

Some requests will see a double redirect. That's not such a good idea.

Your first redirect doesn't cope with appended port numbers on the end of the domain if the domain is www. Those non-canonical requests should also be redirected to www without port number.

Redirects should always target a domain name. Your second rule doesn't do that.

Your third rule allows multiple URLs to fetch the same content. That's a Duplicate Content issue that needs fixing.

Does this code pertain to a single domain, or is it covering multiple domains with the one chunk of code?


 5:50 pm on Apr 23, 2010 (gmt 0)

Thanks for the reply g1smd.

I found some code on askapache.com that is supposed to combine the add www and add the / without double redirects, but i could never get it to work:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\(/)(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)..+/(www.)|.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(#)?(/)?(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)..+/(www.))$ [NC]
RewriteRule ^ http://%6%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]

The last rule uses the two variables to load different pages in a template system.


 8:29 pm on Apr 23, 2010 (gmt 0)

It seems you're trying to copy-and-paste code -- a potentially dangerous endeavor...

Because every site is different and there are hundreds (if not thousands) of so-called "standard" server configurations, the probability of being able to find "just the right code" already posted on-line is essentially zero.

While specific questions are welcome here, we cannot write and debug your code for you -- we do not have enough volunteers to do that, I'm afraid. So I'd like to ask that you make an effort to understand the code you propose to use, because only you know enough about your site to determine if it is appropriate. The resources cited in our Apache Forum Charter, the tutorials and example threads in our Apache Forum Library, and the site search facility will all likely be useful in this regard.

This will benefit the discussion greatly by bringing it some focus, preventing wasted effort on previously-well-discussed topics, and will benefit you by possibly preventing you from using subtly-incorrect code that can ruin your search rankings over time and put you out of business...



 12:32 am on Apr 24, 2010 (gmt 0)

I asked:

Does this code pertain to a single domain, or is it covering multiple domains with the one chunk of code?


 3:24 pm on May 6, 2010 (gmt 0)

g1smd, I'm hoping it will be usable on any domain without changing. -- Unless it's better on the server to have "domain.com" in the htaccess file, instead of the server working it out itself? If it's better to just have "domain.com" then thats fine.

I'm really stuck guys :-(

My copy-and-paste attempt is not working too well at all. I wanted the last line to take any either 1 or 2 variables, any more and just show a 404.

RewriteRule ^([0-9a-zA-Z-]+)/?(([0-9a-zA-Z-]+)/)?$ /index.php?p=$1&l=$3 [L]

I tried replacing ([0-9a-zA-Z-]+) with ([.+]+) But it just wouldnt work.

I wanted to be able to have:

"/Us.ser/1name/" or just "/user/" or "/user/name/"

My php code is working fine -- just getting the htaccess working is all i need :-)

Thanks again for your help.

[edited by: jdMorgan at 1:48 pm (utc) on May 7, 2010]
[edit reason] Tidied-up [/edit]


 4:49 pm on May 6, 2010 (gmt 0)

Some confusion between internal rewrites and external redirects is in evidence -- See the thread on the difference between these two in our Apache Forum Library.

Rule order is incorrect, resulting in multiple "stacked" or "chained" redirects if, for example, a request arrives for the non-www hostname with a missing slash.

Supporting arbitrary hostnames complicates things, but here's one implementation:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Define canonical hostname (add "www" if it is missing)
RewriteCond www.%{HTTP_HOST} ^(www\.)?(www\.[a-z-]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^ - [E=MyHostName:%2]
# Externally redirect to add missing trailing slash to requested
# URLs containing no period in the final URL-path-part
RewriteRule ^(([^/]+/)*[^.]*[^./])$ http://%{ENV:MyHostName}/$1/ [R=301,L]
# Externally redirect to canonical "www" subdomains
RewriteCond %{HTTP_HOST} !^(www\.[a-z-]+\.[a-z]{2,6}\.?(:[0-9]+)?)?$ [NC]
RewriteRule ^(.*)$ http://%{ENV:MyHostName}/$1 [R=301,L]
# Internally rewrite all "page" requests to /index.php
RewriteRule ^([0-9a-z\-]+)(/([0-9a-z\-]+))?/$ /index.php?p=$1&l=$3 [NC,L]

Again, I suggest that you do not use this code until you've analyzed it (one character at a time if necessary) using the mod_rewrite and regular-expressions resources cited in our Forum Charter and understand it completely -- both how it affects your server and site, and how it will affect your search results listings. Only you can tell if it does exactly what you want.



 8:01 am on May 7, 2010 (gmt 0)

Jim -- that is working perfect for me :-)

I have searched this forum a lot, and seeing you are the admin and mod of the apache section, and all the great posts you have made - i doubt i'll get a better response then one from you.

Thanks again for your help.


[edited by: jdMorgan at 1:48 pm (utc) on May 7, 2010]
[edit reason] Tidied-up. [/edit]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved