获取特定URL重写规则404

时间:2017-01-17 20:06:48

标签: php .htaccess url-rewriting

我已经学习并编写了一些.htaccess规则,有些规则正在执行。但很少有人没有执行并显示错误或404

这些是规则

RewriteEngine on

# index.php?store=xyz (executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteRule /?index.php$ /%1 [END,R=301]

RewriteRule ^/?([a-zA-Z0-9]+)$ index.php?store=$1 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products$ index.php?store=$1&view=products [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=sales&sale=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END]

# index.php?store=xyz&view=products(executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteRule /?index.php$ /%1/products [END,R=301]

# index.php?store=xyz&view=products&category=123(executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteCond %{QUERY_STRING} category=([^&]+)&?
RewriteRule /?index.php$ /%1/products/%3 [END,R=301]

# index.php?store=xyz&view=sales (error 404)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=sales&?
RewriteRule /?index.php$ /%1/sales [END,R=301]

# index.php?store=xyz&view=sales&sale=123 (error 404)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=sales&?
RewriteCond %{QUERY_STRING} sale=([^&]+)&?
RewriteRule /?index.php$ /%1/sales/%3 [END,R=301]

# index.php?store=xyz&view=single&product=123(executing perfectly)
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=single&?
RewriteCond %{QUERY_STRING} product=([^&]+)&?
RewriteRule /?index.php$ /%1/single/%3 [END,R=301]

你能告诉我我的错误吗?

1 个答案:

答案 0 :(得分:0)

您从

重定向客户端
  ?

的index.php商店= XYZ&安培;图=单&安培;产品= 123

  

/%1 /单/%3

你有一个相应的RewriteRule

RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END]

您还可以从

重定向客户端
  ?

的index.php商店= XYZ&安培;图=销售&安培;售后= 123

  

/%1 /销售/%3

没有对应RewriteRule,只有两个

RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=sales&sale=$2 [END]

因此,将“产品”规则之一更改为“销售”可以解决您的直接问题。

虽然,你应该知道重定向规则没有做,你可能会想到什么。

RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=single&?
RewriteCond %{QUERY_STRING} product=([^&]+)&?
RewriteRule /?index.php$ /%1/single/%3 [END,R=301]

有三个RewriteCond,与您的重写规则中的%1%3不对应,只有%1有效,请参阅RewriteRule获取解释

  

除了纯文本外,Substitution字符串还可以包含

     
      
  1. ...

  2.   
  3. 反向引用(%N)到最后匹配的RewriteCond模式

  4.   

要同时拥有%1%3,您必须在最后RewriteCond中捕获三个部分,例如

RewriteCond %{QUERY_STRING} store=([^&]+)&view=(single)&product=([^&]+)
RewriteRule ...

有关捕获多个部分的解决方案,请参见another answerRewriteCond to match query string parameters in any order