2013-05-02 61 views
1

我在我的网页上有一个重写规则。我的RewriteRule有什么问题?它似乎没问题,但它不起作用

RewriteEngine On 

RewriteRule ^(.*) index.php?p=$1 [L] 

我希望它的工作,因此重写的URL是这样的:

http://example.com   -> index.php 
http://example.com/home  -> index.php?p=home 
http://example.com/lol  -> index.php?p=lol 

但是,当我用我的index.php里面下面的PHP代码

print_r($_GET) 

它给出了这样的:

Array ([p] => index.php) 

它给出了相同的结果在所有的URL(我试过这些:http://example.com,http://example.com/,http://example.com/about,http://example.com/about/

你能帮我debig这个吗?

+0

什么? 'mod_rewrite'离题在这里?我们甚至有一个标签维基! – 2013-05-02 10:56:22

回答

0

我想通弄明白了:

正确的代码是这样的:

RewriteEngine On 
RewriteRule ^([^.]+)/?$ index.php?p=$1 [NC,L] 

对不起,我的问题。

+0

这可以防止您在网址中使用点。不知道功能的错误... – 2013-05-02 10:58:51

0

的问题是,你重写URL仍然符合规则,你会得到一个新的重写:

http://example.com/home 
http://example.com/index.php?p=home 
http://example.com/index.php?p=index.php 

由于[QSA]标志未设置,新p参数取代了以前的一个。 (我不完全确定你为什么没有无限循环,我想mod_rewrite会进行检查以避免无用的重定向)。

您需要添加附加条件。例如,只有当URL与物理文件或目录不匹配时,才可以进行重写:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?p=$1 [L] 
相关问题