首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MOD_REWRITE条件重定向

MOD_REWRITE条件重定向
EN

Stack Overflow用户
提问于 2012-11-19 00:24:56
回答 2查看 100关注 0票数 0

我在使用Apache Mod_Rewrite模块时遇到了问题,

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

代码语言:javascript
复制
$country
$state
$location

我成功地在本地重写了url,例如url

代码语言:javascript
复制
localhost/directory/country/state/location /*is redirected to*/
localhost/directory/result.php?c=country&s=state&l=location

我想做的是,我想要重定向

代码语言:javascript
复制
localhost/directory/country to localhost/directory/country.php?c=country

在以下情况下

代码语言:javascript
复制
localhost/directory/country/state to localhost/directory/state.php?c=country&s=state

我正在使用下面的RewriteRule

代码语言:javascript
复制
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ result.php?s=$1&d=$2&l=$3 [L]

如果我想只显示国家/地区页面,如何重写国家/地区和州/省/市/自治区/省/自治区/市/自治区/市/自治区/省/自治区/自治区/市/自治区/省/自治区/自治区/市/自治区/省/自治区。

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

同样的,我将非常感谢你..:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-19 00:44:04

您可以使用向下流来识别URL是否是国家/地区、州或位置,如下所示:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2012-11-19 00:51:58

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

代码语言:javascript
复制
# 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]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13442005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档