我在使用Apache Mod_Rewrite模块时遇到了问题,
我从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我正在使用下面的RewriteRule
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ result.php?s=$1&d=$2&l=$3 [L]如果我想只显示国家/地区页面,如何重写国家/地区和州/省/市/自治区/省/自治区/市/自治区/市/自治区/省/自治区/自治区/市/自治区/省/自治区/自治区/市/自治区/省/自治区。
非常感谢!请通过提供在线教程和其他参考资料来帮助我。
同样的,我将非常感谢你..:)
发布于 2012-11-19 00:44:04
您可以使用向下流来识别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>请注意我是如何从最长、最动态的URL开始的。
虽然我发现在PHP解析端处理一个php页面上的所有动态URL流量更容易,但在你的例子中,像result.php这样的东西,你可以确定输出,而且你不必担心在文件上跳来跳去。
.htaccess
<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/result.php?c=$1&s=$2 [L]
RewriteRule ^localhost/directory/([^/\.]+)/ localhost/directory/result.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
?>发布于 2012-11-19 00:51:58
我认为,你可以使用两条规则:
# 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]https://stackoverflow.com/questions/13442005
复制相似问题