Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

regex - Remove/Redirect index.php from url to prevent duplicate urls

Please read the question carefully before marking as duplicate.

We all know, that using in .htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

we can redirect all traffic to index.php so we can create friendly urls and have one front controller.

Although the question is connected to mod_rewrite the problem is described for Laravel.

The following .htaccess comes by default with Laravel 4 and it works fine:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]


    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.

However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.

For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.

Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.

Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?

Example urls that should be redirected:

  1. mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
  2. mydomain.com/index.php should be redirected to mydomain.com
  3. mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
  4. mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Insert these rules just below RewriteEngine On line:

RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*?)index.php[^/] /$1? [L,R=302,NC,NE]

RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*?)index.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...