Forum Moderators: phranque

Message Too Old, No Replies

How do you 301 redirect index.htm to root?

301 redirecting index file to root

         

killua

7:54 am on Apr 3, 2022 (gmt 0)

10+ Year Member



Hi guys, need some help. I just realized that example.com and example.com/index.htm may be considered duplicate content. So I just thought of doing a permanent 301 redirect from index.htm to root but I don't exactly know how to do it in combination with my existing .htaccess code below:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]


My above .htaccess code is needed to permanently redirect http to https sitewide.

Question, is what do I need to add to also permanently 301 redirect "index.htm" file to the root which is also https://www.example.com/ ?

phranque

9:34 am on Apr 3, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would insert the following before your hostname canonicalization redirect:
RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm
RewriteRule ^index\.htm$ https://www.example.com/ [R=301,L]

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]


you must check THE_REQUEST to avoid a redirect loop from that ruleset.
(i.e. eventually this ruleset will see the internal request for the default directory index document...)

killua

11:00 am on Apr 3, 2022 (gmt 0)

10+ Year Member



Thanks phranque, I've just used your code, and it worked perfectly!

lucy24

5:13 pm on Apr 3, 2022 (gmt 0)

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



you must check THE_REQUEST to avoid a redirect loop from that ruleset.
As an alternative, you can use the [NS] (“no subrequests”) flag. If it works on your server it’s a better option, because the rule then doesn’t have to check Conditions.

killua

12:41 am on Apr 4, 2022 (gmt 0)

10+ Year Member



I see, how do I implement the [NS] (“no subrequests”) flag on my existing code above?

phranque

3:32 am on Apr 4, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



how do I implement the [NS] (“no subrequests”) flag on my existing code above?

RewriteEngine On

RewriteRule ^index\.htm$ https://www.example.com/ [NS,R=301,L]

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

killua

6:00 am on Apr 4, 2022 (gmt 0)

10+ Year Member



Thanks again. I've just implemented that code and it worked perfectly as well on the server.