2010-08-27 94 views
0

我想让mod_rewrite与我的网站一起工作,但由于某种原因,它不工作。 我已经输入代码到我的.htaccess文件中,将非www重定向到www,所以我知道mod_rewrite一般工作。mod_rewrite和php变量

URL的我试图改变是example.com/index.php?p=home因此新的URL将example.com/page/home

然而,当我尝试这个代码,我只是得到一个404告诉我,/页/家不存在。

Options +FollowSymLinks 

RewriteEngine on 

RewriteRule index/p/(.*)/ index.php?p=$1 
RewriteRule index/p/(.*) index.php?p=$1 

任何人都可以帮助我吗?

回答

1

你的模式不匹配您的示例网址。假设你的示例URL是正确的,你需要这个代替:

Options +FollowSymLinks 

RewriteEngine on 

# We want to rewrite requests to "/page/name" (with an optional trailing slash) 
# to "index.php?p=name" 
# 
# The input to the RewriteRule does not have a leading slash, so the beginning 
# of the input must start with "page/". We check that with "^page/", which 
# anchors the test for "page/" at the beginning of the string. 
# 
# After "page/", we need to capture "name", which will be stored in the 
# backreference $1. "name" could be anything, but we know it won't have a 
# forward slash in it, so check for any character other than a forward slash 
# with the negated character class "[^/]", and make sure that there is at least 
# one such character with "+". Capture that as a backreference with the 
# parenthesis. 
# 
# Finally, there may or may not be a trailing slash at the end of the input, so 
# check if there are zero or one slashes with "/?", and make sure that's the end 
# of the pattern with the anchor "$" 
# 
# Rewrite the input to index.php?p=$1, where $1 gets replaced with the 
# backreference from the input test pattern 
RewriteRule ^page/([^/]+)/?$ index.php?p=$1 
+0

这完美的作品。我认为([^ /] +)/?$修复了它。 你能解释一下那个表达的部分,所以我可以更好地理解它吗? – kaotix 2010-08-28 13:31:55

+0

@kaotix - 我现在对规则添加了评论。我不确定你对一般正则表达式有多熟悉,所以我试图尽可能详细地解释。 – 2010-08-28 13:56:28

2

你重写规则使用索引/ P/xxxxx的,但你想/页/ XXXX

尝试

RewriteRule ^/page/(.*)/ index.php?p=$1 
RewriteRule ^/page/(.*) index.php?p=$1 
+0

这是我应该把我原来的帖子。对困惑感到抱歉。 – kaotix 2010-08-28 13:31:03