我使用Apache Mod_Rewrite模块时遇到了问题,
我正在从get请求中检索三个变量说
$country
$state
$location
我成功地在本地重写了网址 例如网址
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]
我如何重写国家和州的情况,如果我只想显示国家页面..
非常感谢!! 请通过提供在线教程和其他参考资料来帮助我。
我非常有责任为你做同样的事情...... :)
答案 0 :(得分:1)
您可以使用向下流动来识别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>
首先请注意我是如何开始使用最长,最动态的网址的。如果您从最短的开始,在您的情况country
,那么URL_Rewrite将首先接受answer
,并且您永远不会遇到其他两个重写。
虽然我发现在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
?>
答案 1 :(得分:0)
我认为,你可以使用两个规则:
# 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]