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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历