htaccess:如果查询字符串中存在点(.),则会生成错误
我编写了 .htaccess 文件,如下所示:
Options +FollowSymLinks
RewriteEngine On
#open the product details page with the Product Number with PN prefix
RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1
#open the product search page for a particular category
RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [NC]
#open the product search page for a particular category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search =$1 [NC]
RewriteRule !\.(html|php)$ - [S=4]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]
RewriteCond %{ENV:uscor} ^Yes$
如果查询字符串值中存在点(.),则无法接受。现在,问题已经解决了,但是不能接受它之前的规则。
此外,如果给出了 URL“URL/products/PN123”或“URL/product/PN123”,我希望它正确重写“URL/product.php?pno=PN123”。请注意,如果使用正确的 CSS 正确给出“URL/PN123”,它可以正确重定向。并且“URL/product/PN123”也可以检索数据,但无法正常显示CSS。
I’ve written .htaccess file as follows:
Options +FollowSymLinks
RewriteEngine On
#open the product details page with the Product Number with PN prefix
RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1
#open the product search page for a particular category
RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [NC]
#open the product search page for a particular category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search =$1 [NC]
RewriteRule !\.(html|php)$ - [S=4]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]
RewriteCond %{ENV:uscor} ^Yes$
It couldn't accept if there is dot(.) in the query string value. Now, it is solved, but can't accept its previous rule.
Furthermore, I want it to rewrite the "URL/product.php?pno=PN123" properly if the URL "URL/products/PN123" or "URL/product/PN123" is given. Please note that it can redirect correctly if "URL/PN123" is given correctly with proper CSS. AND "URL/product/PN123" can also retrieve the data but can't show CSS prperly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您更正/更新的 .htaccess,但是如果写更多关于您的实际需求的信息会更好:
关于您的 css、js、图像文件:确保您的 css、js 等路径以斜杠
/ 开头
而不是一个相对的。This is your corrected/updated .htaccess, however it would have been better if had written more about your actual requirements:
About your css, js, image file: Make sure your path to css, js etc start with a slash
/
and not a relative one.您的正则表达式中有一些错误。如果您想使用,请在您的网站上加载此检查器这些。
模式
^((products/|product/|)PN[0-9-]+)/
与“URL/products/PN123”或“URL/product/PN123”不匹配或“URL/PN123”并由于嵌入的匹配字符串和尾部斜杠而返回 PN123。您可以隐藏产品/并将尾随 / 设为可选,如下所示。我不喜欢使用!规则模式中的前缀,尤其是在使用分组部件时。太容易搬起石头砸自己的脚了。我更喜欢 cond 后跟空规则或否定前瞻。
直到没有匹配,所以我发现总是使用 [L] 标志更安全。
重写映射到真实文件的 URI,所以为什么不将
-f
测试提升到顶部,然后所有其他规则都可以假设此条件为真。将这些放在一起得到:
我不确定您现在想用 %{ENV:REDIRECT_USCOR} 做什么,而无需额外的规则和逻辑,尽管它将作为
$_SERVER["REDIRECT_USCOR']
。You've got a few bugs in your regexps. Load up this checker on your site if you want to play with these.
The pattern
^((products/|product/|)PN[0-9-]+)/
does not match "URL/products/PN123" or "URL/product/PN123" or "URL/PN123" and return the PN123 because of the embedded match string and the trailing slash. You can hide the product(s)/ and make the trailing / optional as below.I don't like using the ! prefix in rule patterns, especially when using a grouped parts. Too easy to shoot yourself in the foot. I prefer a cond followed by a null rule or a negative lookahead.
until no match so I find it just a LOT safer to always use the [L] flag
rewrite URIs which map to real files so why not hoist the
-f
test to the top and all other rules can then assume that this condition is true.Putting these all together gets:
I am not sure what you want to do with %{ENV:REDIRECT_USCOR} now without the additional rules and logic, though it will be available to the script as
$_SERVER["REDIRECT_USCOR']
.