Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
Php MOD_重写条件重定向_Php_Apache_Redirect_Rewrite - Fatal编程技术网

Php MOD_重写条件重定向

Php MOD_重写条件重定向,php,apache,redirect,rewrite,Php,Apache,Redirect,Rewrite,我在使用Apache Mod_重写模块时遇到问题 我正在从get请求中检索三个变量 $country $state $location 我成功地在本地重写了url 例如url localhost/directory/country/state/location /*is redirected to*/ localhost/directory/result.php?c=country&s=state&l=location 我想做的是我想重定向 localhost/directo

我在使用Apache Mod_重写模块时遇到问题

我正在从get请求中检索三个变量

$country
$state
$location
我成功地在本地重写了url 例如url

localhost/directory/country/state/location /*is redirected to*/
localhost/directory/result.php?c=country&s=state&l=location
我想做的是我想重定向

localhost/directory/country to localhost/directory/country.php?c=country
万一

localhost/directory/country/state to localhost/directory/state.php?c=country&s=state
我使用以下规则

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ result.php?s=$1&d=$2&l=$3 [L]
如果我只想显示国家/地区页面,如何重写国家/地区和州/地区

非常感谢!! 请通过提供在线教程和其他参考资料来帮助我


同样,我也非常感谢您……)

您可以使用向下流动来识别URL是国家、州还是位置 比如:

<IfModule mod_rewrite.c>
    Rewrite Engine On
    RewriteRule ^localhost/directory/([^/\.]+)/([^/\.]+)/([^/\.]+)/ localhost/directory/result.php?c=$1&s=$2&l=$3 [L]
    RewriteRule ^localhost/directory/([^/\.]+)/([^/\.]+)/ localhost/directory/state.php?c=$1&s=$2 [L]
    RewriteRule ^localhost/directory/([^/\.]+)/ localhost/directory/country.php?c=$1 [L]
</IfModule>
result.php

<?php
$location = isset($_GET['l']) ? $_GET['l'] : false;
$state    = isset($_GET['s']) ? $_GET['s'] : false;
$country  = isset($_GET['c']) ? $_GET['c'] : false;

// ...PARSE THE REST OF PHP based off of these variables
?>

我认为,您可以使用两条规则:

# localhost/directory/country
RewriteRule ^[^/]+/([^/]+)$  localhost/directory/country.php?c=$1 [L]

# localhost/directory/country/state
RewriteRule ^[^/]+/([^/]+)/([^/]+)$  localhost/directory/state.php?c=$1&s=$2 [L]

用更少的占位符和其他/缩短的替换URL/params复制该规则。非常感谢@samuel。我肯定会利用您建议的文件变量解析概念。。。再次感谢
# localhost/directory/country
RewriteRule ^[^/]+/([^/]+)$  localhost/directory/country.php?c=$1 [L]

# localhost/directory/country/state
RewriteRule ^[^/]+/([^/]+)/([^/]+)$  localhost/directory/state.php?c=$1&s=$2 [L]