URL Mod重写与地区,国家和城市

时间:2014-02-28 07:00:33

标签: apache .htaccess mod-rewrite url-rewriting

我正在尝试制作一个重写规则来执行以下操作:

http://www.domain.com/index.php?region=europe 重写如下: http://www.domain.com/europe

http://www.domain.com/index.php?region=europe&country=france 重写如下: http://www.domain.com/europe/france

http://www.domain.com/index.php?region=europe&country=france&city=paris 重写如下: http://www.domain.com/europe/france/paris

我完全没有头绪,已经尝试了一段时间但没有得到我想要的东西。

任何可以帮助我走向正确方向的人?

谢谢!

2 个答案:

答案 0 :(得分:1)

RewriteRule ^/(europe)$ index.php?region=$1
RewriteRule ^/(europe)/(france)$ /index.php?region=$1&country=$2
RewriteRule ^/(europe)/(france)/(paris)$ /index.php?region=$1&country=$2&city=$3

使它通用:

RewriteRule ^/([^/]+)$ index.php?region=$1
RewriteRule ^/([^/]+)/([^/]+)$ /index.php?region=$1&country=$2
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)$ /index.php?region=$1&country=$2&city=$3

答案 1 :(得分:1)

将此代码放入DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?region=$1&country=$2&city=$3 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?region=$1&country=$2 [L,QSA]

RewriteRule ^([^/]+)/?$ index.php?region=$1 [L,QSA]