Php MOD_重写条件重定向
我在使用Apache Mod_重写模块时遇到问题 我正在从get请求中检索三个变量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
$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]