Forum Moderators: 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

         

vaniaul

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

10+ Year Member



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

omoutop

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

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



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

penders

1:57 pm on Aug 5, 2014 (gmt 0)

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



# 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 (.*)

not2easy

3:55 pm on Aug 5, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.

lucy24

7:02 pm on Aug 5, 2014 (gmt 0)

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



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.)

vaniaul

12:37 pm on Aug 11, 2014 (gmt 0)

10+ Year Member



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.

penders

1:36 pm on Aug 11, 2014 (gmt 0)

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



...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.

not2easy

2:57 pm on Aug 11, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.

vaniaul

11:51 am on Aug 19, 2014 (gmt 0)

10+ Year Member



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

not2easy

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.