Welcome to WebmasterWorld Guest from 34.204.189.171

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Fixing canonical URLs issue via mod-rewrite in .htaccess

http to https, non-www to www, index.php to www

     
12:33 pm on Aug 5, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2004
posts: 93
votes: 0


Hello everyone,

For fixing the canonical URLs for a site running on Apache, I have pulled the following redirection rules for redirecting http to https, non-www to www, index.php to www. Before I forward to the site owner I wanted to get a confirmation from you experts, if you think it is okay. Please have a look below and kindly guide if it will have the desired impact:

# Switch rewrite engine on
RewriteEngine On

# Redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

# Redirect index.php to www
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

Sorry for the repeating the much answered question, as it was coupled with http/https so I though of better clarifying in the absence of a test server.

Warm Regards,
Vani
1:35 pm on Aug 5, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 15, 2004
posts:942
votes: 0


I think you can combine the first two rules into one

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

but i am far from expert to say with certainty
1:57 pm on Aug 5, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3148
votes: 4


# Redirect index.php to www


Note that this only handles the "index.php" in the document root, and does require DirectoryIndex to be set appropriately.

^(.*)$ is the same as (.*)
3:55 pm on Aug 5, 2014 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4515
votes: 350


The Rewrite Condition for the HOST should include the preferred form of your domain so that whatever else comes through gets redirected to that:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

The Rewrite Rule for {THE_REQUEST} needs to precede the rule for www. The www part comes after you take care of the index.php page.
7:02 pm on Aug 5, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15884
votes: 875


On most installations, the index redirect doesn't require a Condition at all. Instead, append the [NS] flag to exclude subrequests. (The same applies to SSIs if you use them. So for example if you've got an /includes/ directory, you can block it unconditionally with a [F,NS] flag.)
12:37 pm on Aug 11, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2004
posts: 93
votes: 0


Thanks to everyone for their guidance. Apparently, I have found an alternative to a 301 redirect in this case for SEO purposes, which is using rel="canonical" tag on secondary pages to direct search engine bots to the main page.
1:36 pm on Aug 11, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3148
votes: 4


...I have found an alternative to a 301 redirect in this case for SEO purposes, which is using rel="canonical" tag...


If you are referring to the cases in your question (which I would assume you are) then a 301 redirect would be preferable to a rel="canonical" tag.
2:57 pm on Aug 11, 2014 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4515
votes: 350


In the question above those are identical, duplicate copies of one page. If the content on both pages is 100% identical, a canonical metatag is not the right solution. Only a 301 will resolve duplicates.

The rel="canonical" tag is for pages with similar but different content, pages that share much in common but have different content. So if that is the case, then 301 is the wrong way to go, but that is not the question that was asked originally.
11:51 am on Aug 19, 2014 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2004
posts: 93
votes: 0


Thanks everyone for your kind advice. Following your kind suggestion not2easy, it is apt for me to do a 301 redirect and I have drafted the following rules, keeping the rule for index.php to www above the non-www to www


# Switch rewrite engine on
RewriteEngine On

# Redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ [domain.co.uk...] [L,R=301]

# Redirect index.php to www
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php [NC]
RewriteRule ^index\.php$ [domain.co.uk...] [L,R=301]

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^(www\.domain\.co\.uk)?$ [NC]
RewriteRule ^(.*)$ [domain.co.uk...] [L,R=301]

Please let me know if you can still spot some errors.

Thanks a lot!

Vani
2:04 pm on Aug 19, 2014 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4515
votes: 350


It could be streamlined as mentioned in a few other comments above, but this should work. I say 'should' because there are environmental factors that you know and we don't.

This index rewrite is only for the root directory, it does not handle [example.co.uk...] so if you need that too, it needs a different line for:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php [NC]

It depends on whether you have everything in the root directory or not, and how you have set Options Indexes.