2011-07-19 11 views
1

現在のステータス: まず、この質問は2日前に投稿した質問に非常に関連していることを指摘したいと思います。私は以来、私が別の問題に直面していた問題に取り組み、さまざまな問題に遭遇しました。私はこれがどんな種類の「ダブルポスト」を構成しないことを望みますか?2つのMod書き換えルールを組み合わせる

欲しい効果を作り出すために孤立して動作するように見える2つのmod書き換えルールを作成しました。これらを以下に示す:

RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?([A-Za-z0-9.-]+)_([A-Za-z0-9.-]*) $1?$2=$3&%1 [L] 

上記のルールは次のようにURLを取る:

www.site.com/param1/thing/sortorder_5/getparam2_6/ 

これに変換:

www.site.com/param1/thing/?sortorder=5&getparam2_6/ 

以下のように、第2の規則である:

RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L] 

これは次のようなURLをとります:

http://www.site.com/cmd/param1/thing/cmd2/param2/ 

とこのに変換:

http://www.site.com?cmd=param1/thing&cmd2=param/ 

望ましい結果:

私は次のことを行うことができるかもしれませんように、これら二つの書き換えルールを統合したいと思います:

http://www.site.com/cmd/param1/cmd2/param2/sortorder_5 

http://www.site.com?cmd=param1&cmd2=param2&sortorder=5 

これを行う方法を教えてくれる人は、一番親切です。私は数日間modのリライトコードをたくさん試してみました。私のregex/modリライトの知識は本当に傷ついているわけではないので、私の謝罪です! modリライトコード全体が下に貼り付けられます。ありがとうございました。

RewriteEngine On 

# This block works fine in isolation 
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?([A-Za-z0-9.-]+)_([A-Za-z0-9.-]*) $1?$2=$3&%1 [L] 

# This block also works fine in isolation 
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L] 

# This block should append the query string to index.php (front controller) 
RewriteCond %{QUERY_STRING} ^(.*)$ 
RewriteRule ^.*$ index.php?%1 [L] 
+0

あなたは、私はちょうど 'のRewriteRule ^(。*)$のような何かをすることをお勧めindex.php''を制御している場合?query = $ 1 [L、QSA] 'を実行し、スクリプト内のクエリを解析します。そうすることで、要求の解析を簡単にすることを除けば、mod_rewriteのルール(他のサーバーでも簡単に使用できます)に依存しません。 – Qtax

+0

Btw、なぜあなたは 'RewriteCond%{QUERY_STRING}(。*)'を使っていますか?書き換えルールで 'QSA'フラグを使うことをお勧めします。 – Qtax

答えて

0

これはテストされ、動作します:

RewriteEngine On 
RewriteRule ^(cmd[^/]*)/([^/]+)/?(.*)$ $3?$1=$2 [QSA,NC,L] 
RewriteRule ^([a-z\d.-]+)_([a-z\d.-]+)/?(.*)$ $3?$1=$2 [QSA,NC,L] 

はフラグに関するmod_rewrite docsを参照してください。

例要求:

cmd42=everything&foo=bar&cmd2=bar&cmd1=foo 

は、この要求があります。そのクエリ文字列を印刷

http://localhost:8080/test/index.pl?cmd42=everything&foo=bar&cmd2=bar&cmd1=foo 
/test/に位置 .htaccess

、およびindex.pl

http://localhost:8080/test/cmd1/foo/cmd2/bar/foo_bar/cmd42/everything 

はで終わりますリダイレクトログ:

[rid#2f0b590/initial] (2) [perdir /root_http/test/] rewrite 'cmd1/foo/cmd2/bar/foo_bar/cmd42/everything' -> 'cmd2/bar/foo_bar/cmd42/everything?cmd1=foo' 
[rid#2f0b590/initial] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/cmd2/bar/foo_bar/cmd42/everything -> /test/cmd2/bar/foo_bar/cmd42/everything 
[rid#2f0b590/initial] (1) [perdir /root_http/test/] internal redirect with /test/cmd2/bar/foo_bar/cmd42/everything [INTERNAL REDIRECT] 
[rid#10dec40/initial/redir#1] (2) [perdir /root_http/test/] rewrite 'cmd2/bar/foo_bar/cmd42/everything' -> 'foo_bar/cmd42/everything?cmd2=bar' 
[rid#10dec40/initial/redir#1] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/foo_bar/cmd42/everything -> /test/foo_bar/cmd42/everything 
[rid#10dec40/initial/redir#1] (1) [perdir /root_http/test/] internal redirect with /test/foo_bar/cmd42/everything [INTERNAL REDIRECT] 
[rid#2e16d90/initial/redir#2] (2) [perdir /root_http/test/] rewrite 'foo_bar/cmd42/everything' -> 'cmd42/everything?foo=bar' 
[rid#2e16d90/initial/redir#2] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/cmd42/everything -> /test/cmd42/everything 
[rid#2e16d90/initial/redir#2] (1) [perdir /root_http/test/] internal redirect with /test/cmd42/everything [INTERNAL REDIRECT] 
[rid#2f0d558/initial/redir#3] (2) [perdir /root_http/test/] rewrite 'cmd42/everything' -> '?cmd42=everything' 
[rid#2f0d558/initial/redir#3] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/ -> /test/ 
[rid#2f0d558/initial/redir#3] (1) [perdir /root_http/test/] internal redirect with /test/ [INTERNAL REDIRECT] 
[rid#2f0eac8/initial/redir#4] (1) [perdir /root_http/test/] pass through /root_http/test/ 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.html 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.shtml 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.cgi 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.fcgi 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.pl 

このログは、サーバーの設定でこれを追加することで見ることができます。

RewriteLog "logs/rewrite.log" 
RewriteLogLevel 2 
+0

残念ながら答えはありませんが、返事には大変感謝しています。 index.phpにURL全体を単に追加し、そのように解析するというあなたの提案が好きでした。はるかに簡単。 これがどのように多くのサーバーで使用できるようになるのでしょうか?確かに、その単一のRewriteRuleが有効になるには、サーバー構成でmodリライトを有効にする必要がありますか? – Pete171

+0

他のサーバーは、そのようなデータをスクリプトに渡す別の方法を持つことができます。あなたはconfigやその他の種類の書き換えルールで設定できます。 – Qtax

+0

@ Pete171、解決を更新し、修正しました。 – Qtax

関連する問題