2014-02-28 159 views
1

我试图做一个重写规则,将做到以下几点:网址国防部重写与地区,国家和城市

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

我完全没有线索,一直在尝试一段时间,但没有得到我想要的。

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

谢谢!

回答

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