問題描述
我正在嘗試使用此信息[從網站復制]重寫我的模式,但它不起作用..有什么問題,請給出正確答案..
I am trying to Rewrite my mode using this info[copy from a website], but it's not working .. what's wrong with it and plz give a correct answer..
Apache重寫引擎主要用于將www.yoursite.com/product.php?id=123等動態url轉為www.yoursite.com/product/123等靜態且用戶友好的url
The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123
RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
另一個例子,重寫自:
www.yoursite.com/script.php?product=123 到 www.yoursite.com/cat/product/123/
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/
RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2
推薦答案
您似乎誤解了重寫模塊(尤其是您的規則)的實際作用.
You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.
很簡單,當您瀏覽到:
localhost/abc/product.php?id=23
RewriteRule 沒有被調用,也不應該做任何事情.這里沒有任何問題,您只是瀏覽了錯誤 URL
The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL
http://www.yoursite.com/product/123 <- URL that user goes to
http://www.yoursite.com/product.php?id=123 <- Rewritten URL that the server sees
解釋了重寫規則
重寫規則分為三個部分(不包括RewriteRule
部分...)
RewriteRule(s) explanined
A rewrite rule is broken down into three parts (not including the RewriteRule
part...)
- 它與 url 匹配的正則表達式
- 它轉換成的網址
- 其他選項
根據您的規則:
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
正則表達式為:^product/([^/.]+)/?$
新網址:product.php?id=$1
選項:[L]
這意味著用戶瀏覽到 nice url http://www.yoursite.com/product/123
(并且您的所有鏈接都指向 nice URL),然后服務器與正則表達式匹配并將其轉換為只有服務器可以看到的新 URL.
This means that the user browses to the nice url http://www.yoursite.com/product/123
(and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.
實際上,這意味著您有兩個 URL 指向同一頁面...nice URL 和 not-nice URL 都將帶您到同一個地方.不同之處在于,不太好/標準 URL 對普通公眾和其他訪問您網站的人是隱藏的.
Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.
http://mysite.com/product/image.jpg
沒有被重定向的原因是因為您在 RewriteRule
中使用了正則表達式.
The reason why http://mysite.com/product/image.jpg
is not being redirected is because of the regex that you use in your RewriteRule
.
^product/([^/.]+)/?$
^
=> 字符串開始product/
=> 匹配文字字符串product/
([^/.]+)
=> 匹配一個或多個字符直到下一個/
或的捕獲組>.
/?$
=> 匹配可選的/
后跟字符串的結尾^
=> Start of stringproduct/
=> Matches the literal stringproduct/
([^/.]+)
=> A capture group matching one or more characters up until the next/
or.
/?$
=> Matches an optional/
followed by the end of the string
給定網址:
http://mysite.com/product/image.jpg
您的正則表達式匹配 product/image
但隨后遇到 .
這會停止匹配...因為那不是字符串 $的結尾code> 規則無效,因此轉換永遠不會發生.
Your regex matches product/image
but then it encounters .
which stops the matching... As that isn't the end of string $
the rule is invalidated and thus the transform never happens.
您可以通過將字符類 [^/.]
更改為 [^/]
或 - 我的 首選 方法 -刪除字符類并簡單地使用 .+
(看到您無論如何都將所有內容捕獲到字符串的末尾
You can fix this by changing your character class [^/.]
to just [^/]
or - my preferred method - to remove the character class and simply use .+
(seeing as your capturing everything to the end of the string anyway
RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]
這篇關于使用 .htaccess 重寫 Mod的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!