Mod_rewrite重定向所有请求

时间:2015-04-22 13:14:30

标签: apache .htaccess mod-rewrite redirect

这应该很简单,但我被卡住了。我想将example.com/something的所有请求重定向到example.com/index.php?code=something,但example.com/add-new除外,这应该转到example.com/index.php?page=add-new

我做了后者

RewriteRule ^add-new$ index.php/?page=add-new [L]

它有效,但我无法弄清楚如何做前者。我希望在最终斜杠之后重定向所有内容,即在example.com之后重定向到index.php?code =

1 个答案:

答案 0 :(得分:0)

我在某种程度上解决了它......

我的脚本使用5个字符的代码,通过反复试验,我发现这有效

RewriteRule ^(.{5})$ index.php/?code=$1 [L]

现在每个包含5个字符的URL都被重定向到应该的位置,或者至少我还没有发现它的问题。重定向只有5个字符串不是我想要的,但它现在完成了这项工作。

这是我完整的.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^add-new$ index.php/?page=add-new [L]
RewriteRule ^(.{5})$ index.php/?code=$1 [L]
相关问题