htaccess:如果查询字符串中存在点(。),则生成错误

时间:2012-02-16 13:40:38

标签: .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$
  1. 如果查询字符串值中有点(。),则无法接受。现在,它已经解决了,但不能接受它以前的规则。

  2. 此外,如果提供URL“URL / products / PN123”或“URL / product / PN123”,我希望它正确地重写“URL / product.php?pno = PN123”。请注意,如果使用正确的CSS正确指定“URL / PN123”,它可以正确重定向。 AND“URL / product / PN123”也可以检索数据,但无法正常显示CSS。

2 个答案:

答案 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,后面是空规则或否定前瞻。

  • 秋季匹配规则可以触发后缀错误,所以我再次避免它们。引擎循环重试.htaccess文件(因为它们是在“ Per Directory 上下文”中评估的) 直到不匹配所以我发现总是使用[L]旗帜
  • 会更安全
  • 再次因为这种循环,您需要确保您的规则将终止,并且更好地以简单透明的方式执行此操作。在这种情况下,你似乎不想 重写映射到真实文件的URI,以便为什么不将-f测试提升到顶部,然后所有其他规则都可以假设这个条件为真。
  • 两个搜索规则意味着如果请求是/ acc123 / etc,则使用cat = acc123参数,否则使用search =。这是你想要的吗?
  • 您需要包含QSA标志,除非要忽略任何现有参数。
  • 环境变量USCOR(我坚持使用大写)在下一次传递时变为REDIRECT_USCOR,所以这是你测试的。我假设规则遵循最后一个cond

将这些全部放在一起得到:

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']使用它。