【问题标题】:Using htaccess to get rid of /index.php link?使用 htaccess 摆脱 /index.php 链接?
【发布时间】:2020-10-21 14:15:50
【问题描述】:

我可以这样打开我的网站:

www.mysite.com

或者像这样:

www.mysite.com/index.php

我想创建一个将www.mysite.com/index.php 重定向到www.mysite.com 的htaccess 规则。但我所有的尝试都有其他副作用。我试过了:

Redirect index.php    home.php
RewriteRule    ^index.php?$    home.php    [NC,L]
RewriteRule    ^index.php/?$    redirect_to_home.php    [NC,L]

但是所有这些都搞乱了原来的 index.php 调用。所以它确实重定向了,但是正常的 mysite.com 链接不再起作用了。

有什么想法吗?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    请您尝试关注一下。

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

    解释:

    • 使RewriteEngine On 使规则生效。
    • RewriteCondREQUEST_FILENAME 提及条件,检查浏览器中是否存在提及的文件。
    • 然后检查THE_REQUEST是否有完整的请求细节(包括URL和GET/POST方法),如果它有index.php。
    • 现在检查 REQUEST_URI 在请求的 url 中是否包含 index\.php,将其之前的所有内容保存到临时缓冲内存中以便稍后检索其值(基本上是其域名)。
    • 最后在RewriteRule 中将带有index.php 的完整URL 重定向到仅根据要求的域名(R=301 用于浏览器端的永久重定向)。

    【讨论】:

      【解决方案2】:

      使用此重定向规则从任何路径中删除/index.php

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 2020-11-08
        • 1970-01-01
        • 2015-11-23
        • 1970-01-01
        相关资源
        最近更新 更多