[英]Mod rewrite using .htaccess
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..我正在尝试使用此信息[从网站复制] 重写我的模式,但它不起作用.. 有什么问题,请给出正确答案..
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 Apache重写引擎主要用于将www.yoursite.com/product.php?id=123等动态url转为www.yoursite.com/product/123等静态且用户友好的url
RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
Another example, rewrite from:另一个例子,重写自:
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/ www.yoursite.com/script.php?product=123 到 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.您似乎误解了重写模块(特别是您的规则)的实际作用。
Quite simply when you browse to:当您浏览到:
localhost/abc/product.php?id=23
The RewriteRule isn't invoked and shouldn't be doing anything. RewriteRule 不会被调用,也不应该做任何事情。 There's nothing wrong here, you're just browsing to the wrong URL
这里没有任何问题,您只是浏览到错误的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
A rewrite rule is broken down into three parts (not including the RewriteRule
part...)重写规则分为三个部分(不包括
RewriteRule
部分...)
Given your rule:鉴于您的规则:
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
The regex is: ^product/([^/.]+)/?$
正则表达式是:
^product/([^/.]+)/?$
The new url : product.php?id=$1
新网址:
product.php?id=$1
The options : [L]
选项:
[L]
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
http://www.yoursite.com/product/123
(并且您的所有链接都指向不错的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.实际上,这意味着您有两个 URL 指向同一个页面……不错的URL 和不不错的URL 都会将您带到同一个地方。 The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.
不同之处在于,对普通公众和其他追求您网站的人隐藏了不美观/标准的URL。
The reason why http://mysite.com/product/image.jpg
is not being redirected is because of the regex that you use in your RewriteRule
.未重定向
http://mysite.com/product/image.jpg
的原因是您在RewriteRule
使用的正则表达式。
^product/([^/.]+)/?$
^
=> Start of string ^
=> 字符串开始product/
=> Matches the literal string product/
product/
=> 匹配文字字符串product/
([^/.]+)
=> A capture group matching one or more characters up until the next /
or .
([^/.]+)
=> 匹配一个或多个字符的捕获组,直到下一个/
或.
/?$
=> Matches an optional /
followed by the end of the string /?$
=> 匹配一个可选的/
后跟字符串的结尾Given the URL:鉴于网址:
http://mysite.com/product/image.jpg
Your regex matches product/image
but then it encounters .
您的正则表达式匹配
product/image
但随后遇到.
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]
尝试
RewriteRule ^product/([a-zA-Z0-9_]+)/?$ product.php?id=$1 [L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.