お世話になります。m(__)m


.htaccessでリダイレクトをしたいのですが、うまく反映されません。

Redirect permanent /example/db/foo.php?cid=160 http://www.example.net/aaa/
Redirect permanent /example/db/foo.php?cid=158 http://www.example.net/bbb/
Redirect permanent /example/db/foo.php?cid=157 http://www.example.net/ccc/

ちなみに、?を外すと反映されています。

Redirect permanent /example/db/viewcat.php http://www.example.net/aaa/

そのものズバリのコードをご教授ください。
よろしくお願いします。

回答の条件
  • URL必須
  • 1人5回まで
  • 登録:
  • 終了:2009/12/17 17:04:39
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:OhYeah No.3

回答回数81ベストアンサー獲得回数14

ポイント100pt
RewriteEngine on

RewriteCond %{QUERY_STRING} ^cid=(.*)&(.*)$
RewriteRule ^example/db/foo.php(.*)$ http://www.example.net/example/db/foo.php/cid/%1/%2?
RewriteCond %{QUERY_STRING} ^cid=(.*)$
RewriteRule ^example/db/foo.php(.*)$ http://www.example.net/example/db/foo.php/cid/%1?

RewriteRule example/db/foo.php/cid/160 http://www.example.net/aaa/
RewriteRule example/db/foo.php/cid/158  http://www.example.net/bbb/
RewriteRule example/db/foo.php/cid/159/123 http://www.example.net/ccc/

このようなコードで実現できます。

RewriteCondの部分で、

http://www.example.net/example/db/foo.php/cid/160

http://www.example.net/example/db/foo.php/cid/158

http://www.example.net/example/db/foo.php/cid/159/123

などに変換しています。

そのあと、Redirect permanentではありませんが、RewriteRuleで個別にリダイレクト先を指定しております。


ただ、結局のところ、

RewriteRule example/db/foo.php/cid/160 http://www.example.net/aaa/

のように、cidに対応してリダイレクト先を決める部分は、70ページ部分書く必要がありますので、そこはがんばってください

id:pocon

カンペキです。ありがとうございます。

>cidに対応してリダイレクト先を決める部分は、70ページ部分書く必要がありますので、そこはがんばってください

やりますやります、全然やります。m(__)m

助かりました。m(__)m

2009/12/17 17:03:40

その他の回答2件)

id:azumi1975 No.1

回答回数337ベストアンサー獲得回数16

ポイント10pt

RedirectMatch を使ってください。

http://q.hatena.ne.jp/answer

id:pocon

RedirectMatch permanent /example/db/foo.php?cid=160 http://www.example.net/aaa/

RedirectMatch permanent /example/db/foo.php?cid=158 http://www.example.net/bbb/

RedirectMatch permanent /example/db/foo.php?cid=157 http://www.example.net/ccc/

としましたが、反映されませんでした。

RedirectMatchでリダイレクトされるコードをご教授いただけますでしょうか?

2009/12/15 21:19:46
id:OhYeah No.2

回答回数81ベストアンサー獲得回数14

ポイント100pt

Redirect permanentや RedirectMatch permanent は、クエリ文字列に対応してないので、mod_rewriteを使うことになります。

コードとしては、↓のような感じで

RewriteEngine on
RewriteCond %{QUERY_STRING} "cid=160"
RewriteCond %{REQUEST_URI} "/db/foo.php"
RewriteRule ^(.*)$ http://www.example.net/aaa/

RewriteCond %{QUERY_STRING} "cid=158"
RewriteCond %{REQUEST_URI} "/db/foo.php"
RewriteRule ^(.*)$ http://www.example.net/bbb/

RewriteCond %{QUERY_STRING} "cid=157"
RewriteCond %{REQUEST_URI} "/db/foo.php"
RewriteRule ^(.*)$ http://www.example.net/ccc/

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

id:pocon

ありがとうございます。

リダイレクトが必要なページが70ほどあり、一つづつするにはかなり労力が必要で。。

あと、その後に&もあり、これも含めて処理をする必要があるのですが

たとえば、

http://www.example.net/example/db/foo.php?cid=160

http://www.example.net/example/db/foo.php?cid=158

http://www.example.net/example/db/foo.php?cid=159&123

となっているものを

http://www.example.net/example/db/foo.php/cid/160

http://www.example.net/example/db/foo.php/cid/158

http://www.example.net/example/db/foo.php/cid/159/123

のように一括してRewrite後

Redirect permanent /example/db/foo.php/cid/160 http://www.example.net/aaa/

Redirect permanent /example/db/foo.php/cid/158 http://www.example.net/bbb/

Redirect permanent /example/db/foo.php/cid/159/123 http://www.example.net/ccc/

としたRedirectってできるのでしょうか?

可能であれば、度々で申し訳ないのですがそのRewriteのコードをご教授いただけますでしょうか?

2009/12/15 22:29:47
id:OhYeah No.3

回答回数81ベストアンサー獲得回数14ここでベストアンサー

ポイント100pt
RewriteEngine on

RewriteCond %{QUERY_STRING} ^cid=(.*)&(.*)$
RewriteRule ^example/db/foo.php(.*)$ http://www.example.net/example/db/foo.php/cid/%1/%2?
RewriteCond %{QUERY_STRING} ^cid=(.*)$
RewriteRule ^example/db/foo.php(.*)$ http://www.example.net/example/db/foo.php/cid/%1?

RewriteRule example/db/foo.php/cid/160 http://www.example.net/aaa/
RewriteRule example/db/foo.php/cid/158  http://www.example.net/bbb/
RewriteRule example/db/foo.php/cid/159/123 http://www.example.net/ccc/

このようなコードで実現できます。

RewriteCondの部分で、

http://www.example.net/example/db/foo.php/cid/160

http://www.example.net/example/db/foo.php/cid/158

http://www.example.net/example/db/foo.php/cid/159/123

などに変換しています。

そのあと、Redirect permanentではありませんが、RewriteRuleで個別にリダイレクト先を指定しております。


ただ、結局のところ、

RewriteRule example/db/foo.php/cid/160 http://www.example.net/aaa/

のように、cidに対応してリダイレクト先を決める部分は、70ページ部分書く必要がありますので、そこはがんばってください

id:pocon

カンペキです。ありがとうございます。

>cidに対応してリダイレクト先を決める部分は、70ページ部分書く必要がありますので、そこはがんばってください

やりますやります、全然やります。m(__)m

助かりました。m(__)m

2009/12/17 17:03:40

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません