【问题标题】:Getting 404 on specific URL rewrite rule在特定的 URL 重写规则上获取 404
【发布时间】:2017-06-02 00:56:27
【问题描述】:

我已经学习并编写了一些.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]

你能告诉我我做错了什么吗?

【问题讨论】:

  • 检查代码!我写了(executing perfectly)(error 404),它们的规则写在下面

标签: php .htaccess url-rewriting


【解决方案1】:

您从

重定向客户端

index.php?store=xyz&view=single&product=123

/%1/single/%3

你有一个对应的RewriteRule

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

您还从

重定向客户端

index.php?store=xyz&view=sales&sale=123

/%1/sales/%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

除了纯文本,替换字符串还可以包含

  1. ...

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2013-06-08
    • 2015-10-23
    • 2012-04-27
    • 2011-10-07
    相关资源
    最近更新 更多