【问题标题】:htaccess: generates error if dot(.) exists in query stringhtaccess:如果查询字符串中存在点(。),则生成错误
【发布时间】:2012-02-16 13:40:38
【问题描述】:

我已经编写了 .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。

【问题讨论】:

  • 什么发送Object not found? search.php 查询?
  • 你能不能只在字符类中添加一个点? ^([A-Za-z0-9-_.]+)/?$
  • @Michael,我添加了一个点,但它不起作用。在这种情况下,它将进入空白页面而不是错误消息。
  • 如果您有空白页,请在 PHP 中打开 display_errorsini_set('display_errors', 1);你可能有语法错误或未过滤的 SQL 查询什么的......
  • 是的,display_errors 已开启。在这种情况下,它不会产生 PHP 错误。它只是挂在空白页上。

标签: regex apache .htaccess mod-rewrite


【解决方案1】:

这是您更正/更新的 .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、图像文件:确保您的 css、js 等路径以斜杠 / 开头,而不是相对路径。

【讨论】:

  • 谢谢。也许,你的回答会解决我的问题。但是现在,唯一的问题是:任何带有 URL(例如:“URL/aboutus.html”)的东西都会重定向到搜索框中带有“aboutus.html”的搜索页面。
  • 当你说URL/aboutus.html/URL/about.html路径中有物理文件吗?如果是,那么它不应该重定向到搜索页面,因为它之前还有 RewriteCond %{REQUEST_FILENAME} !-f 行,基本上说如果它不是物理文件,则应用下一个重写器。
【解决方案2】:

您的正则表达式中有一些错误。如果您想玩这些,请在您的网站上加载 this checker

  • 模式 ^((products/|product/|)PN[0-9-]+)/ 不匹配“URL/products/PN123”或“URL/product/PN123”或“URL/PN123”并返回 PN123,因为嵌入了匹配字符串和尾部斜杠。您可以隐藏产品/并使尾随/可选,如下所示。

  • 我不喜欢使用 !规则模式中的前缀,尤其是在使用分组部件时。太容易射中自己的脚了。我更喜欢 cond 后跟 null 规则或否定前瞻。

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

把这些放在一起得到:

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'] 用于脚本。

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2012-10-15
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    相关资源
    最近更新 更多