从 url 中删除/重定向 index.php 以防止重复的 url [英] Remove/Redirect index.php from url to prevent duplicate urls

查看:21
本文介绍了从 url 中删除/重定向 index.php 以防止重复的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标记为重复之前,请仔细阅读问题.

我们都知道,在.htaccess中使用:

We all know, that using in .htaccess:

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

我们可以将所有流量重定向到 index.php,这样我们就可以创建友好的 url 并拥有一个前端控制器.

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.

以下 .htaccess 默认是 Laravel 4 自带的,它工作正常:

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 url 创建我们将没有带有 index.php 的 url,但还有一些其他问题.

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/somethingmydomain.com 的网址中/index.php/something2 等等,搜索引擎会看到重复的网址.

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?

应该重定向的示例网址:

Example urls that should be redirected:

  1. mydomain.com/index.php/something 应该被重定向到 mydomain.com/something(东西可以是任何东西 - 可以包含任何字符)
  2. mydomain.com/index.php 应该重定向到 mydomain.com
  3. mydomain.com/index.php?anything 应该重定向到 mydomain.com(任何内容都可以包含任何字符)
  4. mydomain.com/index.phpanything 应该重定向到 mydomain.com 任何东西都可以包含任何字符)
  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)

推荐答案

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]

这篇关于从 url 中删除/重定向 index.php 以防止重复的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆