从网址中删除/重定向index.php以防止重复的网址 [英] Remove/Redirect index.php from url to prevent duplicate urls
问题描述
在标记为重复项之前,请仔细阅读问题.
我们都知道,在.htaccess
中使用:
We all know, that using in .htaccess
:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
我们可以将所有流量重定向到index.php
,因此我们可以创建友好的网址并拥有一个前端控制器.
we can redirect all traffic to index.php
so we can create friendly urls and have one front controller.
尽管问题与mod_rewrite
有关,但问题是针对Laravel进行的.
Although the question is connected to mod_rewrite
the problem is described for Laravel.
Laravel 4默认随附以下.htaccess
,它可以正常工作:
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>
如果我们运行url mydomain.com/something
并为something
正确设置了该路由,则将启动某些控制器.到目前为止,一切正常.
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.
但是在Laravel 4
中,我们将能够使用mydomain.com/index.php/something
到达相同的路线.可能使用Laravel网址创建,我们将在网址中没有带有index.php
的网址,但是还有其他问题.
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.
例如,如果我们的竞争对手希望对我们造成伤害,他们可以简单地将Internet单一链接放入指向mydomain.com/index.php/something
,mydomain.com/index.php/something2
等的URL,搜索引擎将看到重复的URL.
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.
当然,如果我们有自定义的PHP应用程序,我们可以在PHP中完成它,而只需检查$_SERVER['REQUEST_URI']
并进行301重定向就不会出现问题.我们当然可以在Laravel中做同样的事情,但是每次都必须用PHP编写此代码,可能有些开发人员可能会说用PHP来做是不好的做法.
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.
问题很简单:如何在.htaccess中将所有包含index.php的URL重定向到没有index.php的相同URL?
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
应重定向的示例网址:
-
mydomain.com/index.php/something
应该重定向到mydomain.com/something
(可以是任何东西-可以包含任何字符) -
mydomain.com/index.php
应该重定向到mydomain.com
-
mydomain.com/index.php?anything
应该重定向到mydomain.com
(任何字符都可以包含任何字符) -
mydomain.com/index.phpanything
应该重定向到mydomain.com
任何可以包含任何字符的字符)
mydomain.com/index.php/something
should be redirected tomydomain.com/something
(something could be anything - can contain any characters)mydomain.com/index.php
should be redirected tomydomain.com
mydomain.com/index.php?anything
should be redirected tomydomain.com
(anything can contain any characters)mydomain.com/index.phpanything
should be redirected tomydomain.com
anything can contain any characters)
推荐答案
在RewriteEngine On
行下面插入这些规则:
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]
这篇关于从网址中删除/重定向index.php以防止重复的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!