.htaccess重写所有“其他”网址

时间:2013-02-18 11:04:26

标签: apache .htaccess mod-rewrite

希望这很简单。我有一个非常基本的.htaccess,它会将对/admin(或/admin/etc)的任何请求重写为/_admin/index.php。到目前为止一切顺利:

#Options All -Indexes

RewriteEngine On
RewriteBase /admin

RewriteRule ^admin/$ /_admin/index.php [QSA]
RewriteRule ^admin$ /_admin/index.php [QSA]
RewriteRule ^admin/(.+)$ /_admin/index.php [QSA]

我还想要的是一个通用的“捕获所有其他”规则来重写任何其他网址(/users/turnips//some/other/path等等)回到/index.php

我似乎无法让它工作 - 它的server error 500/admin也会被重写到根页面。我确信我只需要对RewriteCond做点什么,但我无法理解。

谢谢!

2 个答案:

答案 0 :(得分:1)

在其他规则之后添加此项。如果未应用先前的规则,那将是默认规则。

RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule .*        index.php        [L,QSA]

答案 1 :(得分:0)

首先,我建议您在重写时添加 L 标志,以便在匹配重写后确保避免意外匹配(当然除非有意)。

其次,WordPress使用以下代码重写与 index.php 或已存在的文件不匹配的所有URL。这样,不会重写直接访问的文件,如图像,文本文件,下载等。请注意,最初它还包含行RewriteCond %{REQUEST_FILENAME} !-d也不会重写目录,但您似乎想要这种行为:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php - [L]

请查看这是否符合您的需求。