2010-08-27 51 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