【问题标题】:.htaccess rule to remove index.php at the end of urls.htaccess 规则删除 url 末尾的 index.php
【发布时间】:2015-01-13 02:15:42
【问题描述】:

大家好,有人可以帮助我使用 .htaccess 规则来删除网址末尾的 /index.php

如果有人尝试访问的示例:

the-url-address.html/index.php

另一个网址地址.html/index.php

他们应该被重定向到:

-url-address.html

另一个网址地址.html

从 url 末尾删除 /index.php

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此规则:

    # set index.php as the default handler
    DirectoryIndex index.php
    
    RewriteEngine On
    
    # remove index.php from the end of URI
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
    

    【讨论】:

    • 使用它将所有以 /index.php 结尾的 url 重定向到网站主页,我不希望这样,我希望它们被重定向到 /index.php 部分之前的 url
    • 以上也会将http://site.com/dir/index.php重定向到http://site.com/dir
    • 对我来说将以 /index.php 结尾的所有内容重定向到网站主页
    • 如果您只提供http://site.com/index.php,它将被重定向到http://site.com/,但如果您输入:http://site.com/somedir/index.php,那么它将被重定向到:http://site.com/somedir
    • 但如果我提供site.com/some-url.html/index.php,它将重定向到site.com,我希望它转到site.com/some-url.html
    【解决方案2】:

    这在我的问题中已解决,您需要添加

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

    就在 RewriteEngine On call 的下方

    示例:

    ########## Begin - RewriteEngine enabled
    RewriteEngine On
    ########## End - RewriteEngine enabled
    
    ########## remove index.php from the end of URI
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
    ##########
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2017-07-22
      • 2016-09-11
      • 1970-01-01
      相关资源
      最近更新 更多