1

我有一个mysite.com/?local=1本地查询字符串从 1 到 600 不等。

每个本地都是一个城市名称。如何重写 url 以使其显示mysite.com/cityname/。我不介意为每个场景编写一个场景,例如 if (local ==1){cityname = boston;}。

我所要求的甚至可能吗?所以我想把mysite.com/?local=1它变成mysite.com/boston/.

4

2 回答 2

0

如果您希望 URL 在浏览器中实际更改,则需要重定向而不是重写。在这种情况下,您可以从号码中查找名称并header("Location...");根据需要使用。

于 2012-09-18T18:50:32.510 回答
0

索引.php:

<?php
$map = array(
    1 => 'city',
    2 => 'Portland',
    3 => 'Kaunas',
    4 => 'Hokaido',
    .....
);
if (isset($_GET['local']) && intval($_GET['local']) < 600 && intval($_GET['local']) > 0) {
    header('Location: mysite.com/' . $map[intval($_GET['local'])]);
    die(); // Redirect to use the pretty url
}
$path = explode('/', explode('?', $_SERVER['REQUEST_URI'])[0]); // might need to expand if php version < 5.4
$city = $path[1];

.ht 访问:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [PT,L]
</IfModule>

Basically what this combo does is if a GET string with variable 'local' is passed it will do a redirect with the city id converted to a name. After that you just take the query string and do whatever you want with it

于 2012-09-18T19:05:14.463 回答