【问题标题】:How to redirect to relative url htaccess apache 2.22如何重定向到相对 url htaccess apache 2.22
【发布时间】:2016-12-12 08:01:50
【问题描述】:

我正在尝试将rootdomain.com/index.php 重定向到rootdomain.com,并且只是根目录,而不是任何以index.php 结尾的子目录

我试过了

Redirect 302 ^(.*)index.php$ /$1/

Redirect 302 ^(.*)index.php$ $1/

RewriteRule ^(.*)index.php$ $1/ [R=302,L]

RewriteRule ^(.*)index.php$ / [R=302,L]

RewriteRule ^(.*)index.php$ $1/ [R=302,L,QSD]

RewriteRule ^(.*)index.php$ / [R=302,L,QSD]

(我意识到还有其他类似的问题。但是其他问题中的解决方案对我不起作用)

【问题讨论】:

    标签: .htaccess apache2 url-redirection


    【解决方案1】:

    您可以使用此通用规则从任何相对或根路径中删除 index.php

    # remove index.php from root
    RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
    RewriteRule ^ / [L,R=301,NE]
    

    【讨论】:

    • 非常感谢。 apache 语法相当复杂。似乎没有必要。
    • 在第 3 行,^(.*)/index.\.php$ 包含的正则表达式似乎应该针对以 index.php 结尾的任何 url,但它仅在 url 为 {root}/index.php 时重定向。我实际上更喜欢后一种行为,但我觉得(.*) 似乎没有效果
    • 其实还有一个小问题,清除浏览器缓存后立即尝试。
    • 是为了捕获目标中的/ 而进行的更改,还是旨在捕获子文件夹?在我的测试中,它不会影响子文件夹 - 它只影响我想要的 root/index.php url。
    • 非常感谢,@anubhava
    【解决方案2】:

    在您的 .htaccess 中尝试此操作,它应该会从您的 URL 中删除 index.php

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 1970-01-01
      • 2013-07-04
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多