【问题标题】:Remove/Redirect index.php from url to prevent duplicate urls从 url 中删除/重定向 index.php 以防止重复的 url
【发布时间】:2014-09-26 04:05:55
【问题描述】:

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

我们都知道,使用.htaccess:

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

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

虽然该问题与mod_rewrite 有关,但该问题已针对 Laravel 进行了描述。

以下.htaccess 默认随 Laravel 4 提供,并且可以正常工作:

<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 正确设置了该路由,则会启动一些控制器。到目前为止它运行良好。

但是在Laravel 4 中,我们将能够使用mydomain.com/index.php/something 到达相同的路线。可能使用 Laravel url 创建我们将没有带有 index.php 的 url 但还有一些其他问题。

例如,如果我们的竞争对手想对我们造成一些伤害,他们可以简单地为mydomain.com/index.php/somethingmydomain.com/index.php/something2等的网址添加互联网单一链接,搜索引擎会看到重复的网址。

当然,如果我们有我们的自定义 PHP 应用程序,我们可以在 PHP 中执行它而不会出现问题,只需检查 $_SERVER['REQUEST_URI'] 并进行 301 重定向。我们当然可以在 Laravel 中做同样的事情,但我们每次都必须用 PHP 编写这段代码,可能有些开发人员会说用 PHP 编写是不好的做法。

问题很简单:如何在 .htaccess 中将所有包含 index.php 的 url 重定向到没有 index.php 的相同 url?

应重定向的示例网址:

  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 任何东西都可以包含任何字符)

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    RewriteEngine On 行下方插入这些规则:

    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]
    

    【讨论】:

    • 我在这里发现了一些问题。 1)我不明白为什么两个规则中的 RewriteCond 是相同的。 2)mydomain.com/index.php/something 重定向到 mydomain.com//something(双斜杠)3)mydomain.com/index.php?anything 重定向到 mydomain.com/?anything 而不是 mydomain.com 其他 2 条规则 mydomain.com/index.phpanythingmydomain.com/index.php 按预期工作
    • 现在检查更新的规则。 RewriteCond 行相同,但 URI 匹配模式在 2 条规则中不同。
    • 不客气,很高兴它成功了。请记住将302 更改为301 以使其永久重定向。
    • 是的,我注意到您使用了 302 重定向而不是 301。但顺便说一下,正如我在这个问题中所展示的那样,框架根本不关心重复的 url,这很奇怪。默认情况下,他们应在其.htaccess 文件中提供这些(或类似)规则
    • 只想说,这段代码在2020年完美运行。Laravel 6.x
    【解决方案2】:

    如何在 .htaccess 中重定向所有包含 index.php 的 url 到 没有index.php的同一个url?

    将此添加到您的 .htaccess 中

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ %1 [R=301,L]
    

    【讨论】:

    • 对我来说它不起作用。 mydomain.com/index.php 将我的 url 重定向到 mydomain.com/home/testsite/public_html 以便在 url 中显示我的文件路径
    【解决方案3】:

    对于 Nginx,这里是规则:

    location / {
        rewrite ^/(.*?)index\.php[^/] /$1? redirect;
        rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
    }
    

    【讨论】:

      【解决方案4】:

      这解决了我在 Kohan 2.3 中强制 https 并从 url 中删除 index.php 的问题

      RewriteEngine On
      
      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]
      
      
      RewriteRule ^(application|system) - [F,L]
      
      RewriteCond %{THE_REQUEST} /index.php [NC]
      RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      
      RewriteRule .* index.php/$0 [PT,L]
      
      
      RewriteCond %{HTTPS} off [OR]
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
      RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
      

      【讨论】:

        【解决方案5】:

        花了几个小时后,我为我写了下面的代码,它 100% 工作

        将 index.php 重定向到非 index.php

        RewriteCond %{THE_REQUEST} ^.*/index\.php
        RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
        

        【讨论】:

          猜你喜欢
          • 2021-05-10
          • 1970-01-01
          • 1970-01-01
          • 2014-03-08
          • 2015-03-19
          • 2020-08-12
          • 1970-01-01
          • 2011-12-03
          • 2017-05-22
          相关资源
          最近更新 更多