Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Mod Rewrite-使用混合url结构_Regex_.htaccess_Mod Rewrite - Fatal编程技术网

Regex Mod Rewrite-使用混合url结构

Regex Mod Rewrite-使用混合url结构,regex,.htaccess,mod-rewrite,Regex,.htaccess,Mod Rewrite,我有一个要求重写一个网址,但没有一个确切的顺序 例如,我需要能够: www.domain.com/sale www.domain.com/sale/new-york www.domain.com/sale/offices www.domain.com/sale/chicago/villas www.domain.com/new-york www.domain.com/washington/offices 等等。最终,它将不得不转向以下方面: file.php?type=sale file.ph

我有一个要求重写一个网址,但没有一个确切的顺序

例如,我需要能够:

www.domain.com/sale
www.domain.com/sale/new-york
www.domain.com/sale/offices
www.domain.com/sale/chicago/villas
www.domain.com/new-york
www.domain.com/washington/offices
等等。最终,它将不得不转向以下方面:

file.php?type=sale
file.php?type=sale&city=new-york
file.php?type=sale&category=offices
file.php?type=sale&city=chicago&category=villas
file.php?city=new-york
file.php?city=washington&category=offices
所以主要的想法是,我不会有一个特定的子文件夹顺序,我可以用它来构建一个通用规则

我发现这个问题的唯一解决方案是自动生成一个.htaccess,其中包括所有可能的情况和请求的变化(假设类别名称永远不会与城市名称和/或类型相同)

用正则表达式实现这一点还有其他可能性吗

谢谢

我建议您在/.htaccess中进行测试: 我们使用:

\bsale\b: sale is a whole word delimited by word boundary.
\b(chigaco|new-york|washington)\b: cities names must be known.
\b(villas|offices)\b: category names must be predictable.
我已经用这个/file.php进行了测试:

This is file.php
<?php
echo "<pre>";
var_dump($_GET);
echo "</pre>";
?>
如果要处理所有世界城市,则必须进行权衡,城市前缀如下: 即,您必须在HTML文档中使用如下URL:


从我的第一次测试来看,这个解决方案似乎可行。谢谢现在唯一的问题是我可以使用多少个字符作为规则-例如,我可以添加多少个城市到\b(chigaco |纽约|华盛顿)\b?这可能很长,但不是无限的。
This is file.php
<?php
echo "<pre>";
var_dump($_GET);
echo "</pre>";
?>
This is file.php
array(3) {
  ["type"]=>
  string(4) "sale"
  ["city"]=>
  string(8) "new-york"
  ["category"]=>
  string(6) "villas"
}
RewriteEngine On
RewriteRule \b(sale)\b /file.php?%{QUERY_STRING}&type=$1
RewriteRule \b(villas|offices)\b /file.php?%{QUERY_STRING}&category=$1
RewriteRule \bcity-([\w-]+)\b /file.php?%{QUERY_STRING}&city=$1
http://www.example.com/sale/city-new-york/villas
http://www.example.com/sale/city-beijing/offices
http://www.example.com/sale/villas/city-berlin