Best way to understand Apache Rewrite Implications

webdirexion
webdirexion used Ask the Experts™
on
Hi -

A client has the following rewrite code in his .htaccess file:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Open in new window


I would like to :
a) translate each line into an understanding of what the code does (ie. last line means something like "wildcard redirect all links to this domain to the home page", and I think the first line is about forcing www to be in the domain name.  );

b) understand the options to change it should we judge it to be not the best rewrite for SEO.  Here, I am wondering if someone can point us at a good reference for those that don't write apache code often.

Final question:  what if the domain is going to https -- must that be described in this code?

Thanks -
Scott
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marketing Technologist
Commented:
a) translate each line into an understanding of what the code does (ie. last line means something like "wildcard redirect all links to this domain to the home page", and I think the first line is about forcing www to be in the domain name.  );

Deciphered:

RewriteCond %{HTTP_HOST} !^www\.

Open in new window


Check to see if the url does not have www prepended.

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Open in new window


If previous condition was met, redirect to a version with www. prepended to the url
If previous condition was not met, do nothing.

For example:
If someone types http://yoursite.com in the browser, they'll be redirected to http://www.yoursite.com
If someone types http://www.yoursite.com in the browser, nothing will happen.

b) understand the options to change it should we judge it to be not the best rewrite for SEO.  Here, I am wondering if someone can point us at a good reference for those that don't write apache code often.

From an SEO standpoint, this rule is preventing your site from being indexed with duplicate content.

Google would potentially treat the following pages as duplicates if you didn't have this rule:
http://yoursite.com/page1.htm
and
http://www.yoursite.com/page1.htm

By employing this rule, you eliminate the possibility of a duplicate, because all "non-www" versions of your site are redirected to the www version.

The only change that would make sense would be a flip-flop (ie. redirect all www to the non-www version).

You wouldn't want Google to index both versions of the page, so this acts as a safety measure.


Final question:  what if the domain is going to https -- must that be described in this code?

If you want to force all connections to use https, you will need a different condition and rule combination.
Top Expert 2014

Commented:
Since you do not seem to have any checks for ports (80 vs. 443) I think that if somebody enter https://yoursite.com  they would be redirected to http://www.yoursite.com.

However, if they entered https://www.yoursite.com, there would be no re-write because the hostname starts with www.

Author

Commented:
This was a well-crafted answer.  Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial