Question

Have a website with subpages all in this format:

mydomain.com/something

That's fine. But what is NOT fine is that you can also do mydomain.com/something/index.php (you can enter address in this format into your browser) and you still get the content on that mydomain.com/something.

I don't want those two possibilites to be available at the same time, Google doesn't like this. I want just one to be possible.

So what I want to do is whenever you type into your browser mydomain.com/something/index.php, you will be redirected to mydomain.com/something (without that /index.php at the end).

How should I write a .htaccess code to do something like this?

Was it helpful?

Solution

add the following lines to .htaccess in the root directory of your website

RewriteEngine on

RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*?)/?index.php$ /$1 [R=301,L,NC,QSA]

Note: the first condition assure that no previous redirection is made (to prevent redirection loop)

OTHER TIPS

Mordor:

You can try this in your .htaccess file:

RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top