2016-04-27 5 views
2

こんにちは、htaccess経由でカスタムリダイレクトが必要です。htaccess経由の特別なフォーマット専用のリダイレクト

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

が、他の形式の場合にのみ、このフォーマットの変更なし

:(@starkeenによって)このコード

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

例えばは素晴らしい、それを実行します。

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] 
関連する問題