使用url重写将任何内容重定向到单个php文件

时间:2011-09-30 17:43:31

标签: php url-rewriting

我正在尝试将任何内容重定向到我的index.php文件,以便自己进行重定向。

如果我使用它:

#RewriteRule ^(.+)$ index.php?path=$1%{QUERY_STRING} [L]

但这不起作用,因为它正在“循环”规则。

这有效:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
</IfModule>

但是我想重定向它,即使它是一个现有的文件或目录(我将为图像,js等特殊路由重定向它们与PHP重定向而不加载任何脚本。

事实上,如果有一个“无循环”标志,那就没问题。

有人知道解决方案吗?

由于

2 个答案:

答案 0 :(得分:0)

这是避免循环的一种方法:

RewriteRule ^(?!index\.php)(.+)$ index.php?path=$1%{QUERY_STRING} [L]

使用negative lookahead?!)查看字符串是否包含 index.php 。如果没有,则继续重写!

答案 1 :(得分:0)

用检查替换两个Cond模式,看看你是否正在点击index.php:

RewriteCond %{REQUEST_URI} !^index.php
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]

将重定向所有内容,除了index.php上的命中(以防止循环)。