0

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

我正在从获取请求中检索三个变量说

$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]

我如何重写国家和州的情况,如果我只想显示国家页面..

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

我将非常有义务为您提供同样的.. :)

4

2 回答 2

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>

请注意我是如何首先从最长、最动态的 URL 开始的。如果您从最短的第一个开始,在您的情况下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
?>
于 2012-11-18T16:44:04.410 回答
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]
于 2012-11-18T16:51:58.337 回答