2012-02-16 14 views
4

我写.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”。请注意,如果“URL/PN123”用正确的CSS正确地给了它可以正确重定向。和“URL /产品/ PN123”,也可以检索数据,但不能prperly显示CSS。

+0

什么发送'找不到对象?'search.php查询? – 2012-02-16 13:43:45

+1

你能不能在角色类中加一个点? '^([A-Za-z0-9 -_。] +)/?$' – 2012-02-16 13:44:25

+0

@Michael,我加了一个小点,但没有奏效。在这种情况下,它将变成空白页而不是错误消息。 – Agilox 2012-02-16 13:57:07

回答

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,图片文件:请确保您路径CSS,JS等开始以斜线/,而不是相对的。

+0

谢谢。可能你的答案会解决我的问题。但现在,唯一的问题是:任何带有URL的地址(例如:“URL/aboutus.html”)都会在搜索框中用“aboutus.html”重定向到搜索页面 – Agilox 2012-02-19 08:53:06

+0

当您说'URL/aboutus.html'时是否有物理文件在'/ URL/about.html'路径中?如果是,那么它不应该重定向到搜索页面,因为它也有'RewriteCond%{REQUEST_FILENAME}!-f'行之前基本上说应用下一个重写代码如果它不是一个物理文件 – anubhava 2012-02-20 06:56:41

+0

@wasimchy:既然你已经接受了这个答案,你会奖励赏金吗? – anubhava 2012-02-24 16:20:20

2

你在你的正则表达式中有一些错误。如果你想与这些打在您的网站加载this checker

  • 模式^((products/|product/|)PN[0-9-]+)/不匹配“URL /产品/ PN123”或“URL /产品/ PN123”或“URL/PN123”,并返回,因为嵌入式匹配字符串和结尾的斜线的PN123。您可以隐藏产品/并按照以下方式制作尾随/可选。

  • 我不喜欢用!规则模式中的前缀,特别是在使用分组部分时。太容易把自己踢在脚下。我更喜欢cond后跟无效规则或负向前瞻。

  • 虽然匹配规则可以触发postfix错误,所以我再次避免它们。发动机循环重试.htaccess文件(因为它们是在“每目录上下文”计算) 直到没有比赛,所以我觉得它只是一个安全很多总是使用[L]标志
  • 同样因为这个循环的您需要确保您的规则将终止,并且更好地以简单透明的方式执行此操作。在这种情况下,您似乎不想 重写映射到实际文件的URI,那么为什么不将-f测试提升到最高,然后所有其他规则都可以假设此条件为真。
  • 两个搜索规则意味着猫= acc123参数用于如果请求的是/ acc123 /等,并搜索=否则。这是你想要的吗?
  • 您需要包含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']提供给脚本。