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]
Tomasz
  • 1,288
  • 18
  • 36
  • `index` giving a 200 response might be caused by [`Option MultiViews`](https://httpd.apache.org/docs/2.4/mod/core.html#options), see also [Content Negotiation](https://httpd.apache.org/docs/2.4/content-negotiation.html#multiviews) – Olaf Dietsche Aug 17 '16 at 07:59

1 Answers1

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] 
anubhava
  • 761,203
  • 64
  • 569
  • 643