我写了.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”,它可以正确重定向。 AND“URL / product / PN123”也可以检索数据,但无法正常显示CSS。
答案 0 :(得分:3)
这是您更正/更新的.htaccess,但如果更多地了解您的实际要求会更好:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#open the product details page with the Product Number with PN prefix
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1 [L,NC,QSA]
#open the product search page for a particular category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [L,NC,QSA]
#open the product search page for a particular category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1 [L,NC,QSA]
RewriteRule !\.(html|php)$ - [S=4,NC]
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$
关于你的css,js,image文件:确保你的css,js等路径以斜线/
开头,而不是相对的斜杠。
答案 1 :(得分:2)
你的regexp中有一些错误。如果您想要使用这些内容,请在您的网站上加载this checker。
模式^((products/|product/|)PN[0-9-]+)/
与“URL / products / PN123”或“URL / product / PN123”或“URL / PN123”不匹配,并返回PN123,因为嵌入的匹配字符串和尾随削减。您可以隐藏产品/并使跟踪/可选如下所示。
我不喜欢使用!规则模式中的前缀,尤其是在使用分组部件时。太容易在脚下射击自己。我更喜欢cond,后面是空规则或否定前瞻。
-f
测试提升到顶部,然后所有其他规则都可以假设这个条件为真。将这些全部放在一起得到:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# The rewrite engine loops in a Per Dir context. We don't remap real files so:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Open the product details page with the Product Number with PN prefix
RewriteRule ^(?:products?)?(PN\d+)/?$ product.php?pno=$1 [L,NC,QSA]
# Open the product search page for a particular category
RewriteRule ^((?:bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [L,NC,QSA]
#open the product search page for a particular category
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1 [L,NC,QSA]
RewriteRule \.(?!(php|html)$) - [S=4,NC]
RewriteRule ^(.*?)_(.*?)_(.*?)_(.*?)_(.*)$ $1-$2-$3-$4-$5 [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*?)_(.*?)_(.*)$ $1-$2-$3-$4 [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*?)_(.*)$ $1-$2-$3 [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*)$ $1-$2 [E=USCOR:Yes, L]
我现在不确定你想用%{ENV:REDIRECT_USCOR}做什么,而没有额外的规则和逻辑,尽管脚本可以$_SERVER["REDIRECT_USCOR']
使用它。