dongshi9407 2014-08-03 14:52
浏览 133
已采纳

从url中删除/重定向index.php以防止重复的URL

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)
  • 写回答

5条回答 默认 最新

  • dongmangsha7354 2014-08-03 15:10
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度