简体   繁体   English

无法使用多个查询字符串重写htaccess

[英]Unable to rewrite htaccess with multiple query strings

I want to rewrite the url with multiple string query 我想用多字符串查询重写网址

Original url 原始网址

www.xxx.com/sale.php?a=sale&&b=product&&c=category

Expected url 预期网址

www.xxx.com/sale/a/b/c

Wrote the below code in htaccess 在htaccess中写了以下代码

RewriteRule ^sale/(.*)/?$ sale.php?a=$1&b=$2&c=$3 

It seems like the / is not escaped, when i tried to echo the value for a, b & c. 当我尝试回显a,b和c的值时,似乎/并未转义。 i got the below output. 我得到以下输出。

 $a = $_REQUEST['a'];
 $b = $_REQUEST['b'];
 $a = $_REQUEST['c'];

Output value
a = sale/product/category
b = 
c = 

Expected result

a = sale
b = product
c = category

Thanks in advance. 提前致谢。

I also tried with the below instance 我也尝试了以下实例

 RewriteRule ^sale/(.*)$ sale.php?a=$1&b=$2&c=$3 

 RewriteRule ^sale/([^/.]+)/?$ sale.php?a=$1&b=$2&c=$3 -> This didnt worked.

 Actual value
 a = sale/product/category
 b = 
 c = 

 Expected result

 a = sale
 b = product
 c = category

This is the actual rule you are looking for: 这是您要寻找的实际规则:

RewriteEngine on
RewriteRule ^/?sale/([^/]+)/([^/]+)/([^/]+)/?$ /sale.php?a=$1&b=$2&c=$3 [END]

Your code snippet suggests that you are using php as a scripting language. 您的代码段表明您正在使用php作为脚本语言。 That means you can access those values as: 这意味着您可以按以下方式访问这些值:

<?php
// ...
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. 如果您使用上述规则收到内部服务器错误(http状态500),则很可能是您运行了非常旧版本的apache http服务器。 You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. 在这种情况下,您将在http服务器错误日志文件中看到对不支持的[END]标志的明确提示。 You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup. 您可以尝试升级或使用旧的[L]标志,在这种情况下它可能会起作用,尽管这在一定程度上取决于您的设置。

This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). 此规则将在http服务器主机配置或动态配置文件(“ .htaccess”文件)中同样起作用。 Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. 显然,重写模块需要加载到http服务器内部并在http主机中启用。 In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder. 如果使用动态配置文件,则需要注意在主机配置中完全启用了它的解释,并且该解释位于主机的DOCUMENT_ROOT文件夹中。

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). 还有一个一般性说明:您应该始终喜欢将此类规则放在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。 Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. 这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。 They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare). 仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM