0

This code in my .htaccess 301-redirects: index.html and index.php to root (/):

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ /$1 [R=301,L]

The problem is, many web servers treat 'index' (without .html or .php) as a valid request; in result: example.com/index gives 200 status (which could create duplicate content).

So my goal is to use my current code (above) and add to it so that also 'index' is 301-redirected to root. In other words, the code should redirect:

example.com/index
example.com/index.html
example.com/index.php

to root (ie. example.com)

I tried this below but it's too complicated for me to make it correctly (I do prefer to add to the current code so that there is only one redirection code, not multiple RewriteCond / RewriteRule:

(This one is not working!)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index$|([^/]+*index)\.(html?|php))\ HTTP/
RewriteRule ^(index$|(([^/]+/)*)index\.(html?|php))$ /$1 [R=301,L]
1

1 Answer 1

2

You can tweak your regex to make it match /index or /index.html or /index.php.

Replace your rule with this:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index(\.(html?|php))?\ HTTP/ [NC]
RewriteRule ^((?:[^/]+/)*)index(\.(html?|php))?$ /$1 [R=301,L,NC,NE] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.