【问题标题】:How to mod-rewrite all requests to single file without causing an infinite loop如何在不导致无限循环的情况下将所有请求修改为单个文件
【发布时间】:2012-09-25 23:43:00
【问题描述】:

如何将所有请求发送到 www.myurl.com/{ANYTHING} 并将它们全部发送到 www.myurl.com/index.php

我发现我可以通过以下方式发送所有内容:

RewriteRule .* index.php [R=Permanent,L]

这很好用,除了因为我的 cpanel/apache 安装而被重定向到 www.myurl.com/home/username/public_html。因此,我将代码更改为

RewriteBase /
RewriteRule .* index.php [R=Permanent,L]

但这又会导致无限循环。

【问题讨论】:

    标签: apache mod-rewrite cpanel whm


    【解决方案1】:

    试试:

    RewriteCond %{REQUEST_URI} !^/index.php
    RewriteRule .* /index.php [R=Permanent,L]
    

    【讨论】:

    • 这似乎将所有内容重定向到 index.php,但在 index.php 中使用 phpinfo() 表明无法获取原始 URL。所以这个重定向,但原来的 URL 丢失了。
    【解决方案2】:

    对于 Apache,使用 mod_rewrite:

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

    【讨论】:

    • 这似乎也失去了原来的网址(x.php)。
    【解决方案3】:

    这有效地将我发送到 index.php 而不会导致循环。

    RewriteCond %{REQUEST_URI} ^/index\.php$
    RewriteRule ^(.*)$ - [L]
    
    RewriteRule ^(.*)$ /index.php?url=%{REQUEST_URI} [R=302,L,QSA]
    

    【讨论】:

      猜你喜欢
      • 2011-11-20
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      相关资源
      最近更新 更多