2016-04-27 28 views
2

嗨那裏我想通過htaccess自定義重定向。自定義重定向僅用於通過htaccess的特殊格式

僅此格式

www.example.com/index.php?id=abc --> www.example.com/abc 

但對於任何其他格式沒有變化:

例如

www.example.com/index.php?id=abc&id2=qaz --> www.example.com/index.php?id=abc&id2=qaz 

此代碼(由@starkeen)做它的偉大:

RewriteEngine on 

#1)Redirect "/index.php?id=foo" to "/foo"# 
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)\sHTTP [NC] 
RewriteRule^/%1? [L,R] 
#2)The rule bellow will internally map "/foo" to "/index.php?id=foo"# 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L] 

現在如何添加一個自定義參數名稱,如id2(或更多)?

例如

www.example.com/index.php?id=abc --> www.example.com/abc 

www.example.com/index.php?id=abc&id2=qaz --> www.example.com/abc/qaz 

但是(在這裏)這種沒有變化:

www.example.com/index.php?id=abc&id2=qaz&id3=wsx --> www.example.com/index.php?id=abc&id2=qaz&id3=wsx 

回答

1

你需要另一套規則來處理2個查詢參數:

RewriteEngine On 

#1A) Redirect "/index.php?id=foo" to "/foo" 
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)\sHTTP [NC] 
RewriteRule^/%1? [L,R] 

#1B) Redirect "/index.php?id=foo&id2=bar" to "/foo/bar" 
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)&id2=([^\s&]+)\sHTTP [NC] 
RewriteRule^/%1/%2? [L,R] 

# skip all the files and directories from further rules 
RewriteCond %{REQUEST_FILENAME} -d [OR] 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule^- [L] 

#2A) The rule bellow will internally map "/foo/bar" to "/index.php?id=foo&id2=bar" 
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?id=$1&id2=$2 [L,QSA] 

#2B) The rule bellow will internally map "/foo" to "/index.php?id=foo" 
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L,QSA]