0

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?

Mordor
  • 485
  • 2
  • 4
  • 14

2 Answers2

0

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)

Amine Hajyoussef
  • 4,381
  • 3
  • 22
  • 26
  • what other rules do you have in your .htaccess, there must be some conflit. – Amine Hajyoussef Jul 06 '13 at 19:26
  • Only `www` to `non-www` redirect, oh plus this `RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$ index.php [L]` – Mordor Jul 06 '13 at 19:46
  • Oh yes, now your solution works when I remove lines above (that `RewriteCond %{REQUEST_FILENAME} !-f ...`, but unfortunatelly then mydomain.com/something makes that /something page a 404 - I need that previous lines there, can't remove them... – Mordor Jul 06 '13 at 19:52
  • add the code in my answer before those directives, that should fix it – Amine Hajyoussef Jul 06 '13 at 19:58
0

Mordor:

You can try this in your .htaccess file:

RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Joel Hernandez
  • 2,195
  • 1
  • 13
  • 17
  • Your solution makes `mydomain.com/anything/index.php` goes to homepage, to `mydomain.com` which is not what my heart desires. I need it to go to `mydomain.com/anything` (so basically all I need is that `/index.php` to be "escaped") – Mordor Jul 06 '13 at 19:23