2012-05-03 9 views
1

mod_rewriteの正しい指示を作成する際に助けてください。それは非常に難しいようです。mod_rewriteの複雑なルール

私は正確に以下が必要:www.site.comにsite.comから

1. リダイレクト

www.site.com/hello    ==>  site.com/hello 
www.site.com/abc/def    ==>  site.com/abc/def 

などとし、他の規則を適用する必要があります。

2.(などのrobots.txt、画像/、など)いくつかの指定されたファイルやフォルダへの直接アクセス(無リライト)

たぶん

RewriteCond %{REQUEST_URI} !^/(robots.txt|favicon.ico|images|documents|~.\*) 

私は右ですか?

site.com/robots.txt    ==>  site.com/robots.txt 
site.com/documents/file1.pdf  ==>  site.com/documents/file1.pdf 

のようなURLはそのままで、書き換えないでください。

3.別の変換:

site.com/index.php?anythinghere ==>  site.com/a/index.php?anythinghere 

4.しかし、我々は入力した場合のURL site.comとApacheは(site.com/index.php)ルートフォルダでのindex.phpを呼び出す必要がありますsite.com/

site.com       ==>  site.com/index.php 
site.com/       ==>  site.com/index.php 

5.私は、MVCのスクリプトを持っている、と我々は次のURLを入力した場合:

1. site.com/controller or site.com/controller/ or site.com/controller// 
2. site.com/controller/function or site.com/controller/function/ 
3. site.com/controller/function/parameter or site.com/controller/function/param1/param2/param3 

コントローラがリスト内の定義済みの単語の1つ、たとえば "index"、 "news"、 "contacts"(多分数百語に拡張されるかもしれません)の場合は、index.phpを呼び出し、以下の方法:

site.com/controller    ==>  site.com/index.php?q=controller 
site.com/controller/    ==>  site.com/index.php?q=controller/ 
site.com/controller//    ==>  site.com/index.php?q=controller// 
site.com/controller/function  ==>  site.com/index.php?q=controller/function 
site.com/controller/function/  ==>  site.com/index.php?q=controller/function/ 
site.com/controller/function/parameter    ==> site.com/index.php?q=controller/function/parameter 
site.com/controller/function/param1/param2/param3 ==> site.com/index.php?q=controller/function/param1/param2/param3 

6.そして、以前のすべてのルールが適用されなかった場合、最終的に、私たちは希望Apacheが再帰的でmod_rewriteを使用していない、または私は巨大な悩みを持っています

site.com/anythinghere    ==>  site.com/a/index.php?anythinghere 

を書き換える必要があります別のindex.phpで

私はそれが容易ではないことを理解しますが、1つのアイテムでもルールを作成することができれば、それは素晴らしいことです。

ありがとうございます!あなたは.htaccessファイルにこれらの行を置くことができ最初リライトの

+0

私はあなたの質問を少し編集して、変換をより明確にしました。それが役に立てば幸い。 –

+1

ありがとうございました!それは今より良いです。 – user1373539

答えて

0

、:検索エンジンが表示されないように

RewriteCond %{HTTP_HOST} ^www.site.com$ [NC] 
RewriteRule ^(.*)$ http://site.com/$1 [R=301] 

それは、その恒久的なリダイレクトさせるためにR=301を置くことが非常に重要ですwww.site.comおよびsite.comを2つの異なる部位として含む。

第2多くのオプションがあります。実際に存在するすべてのファイル(画像やpdfドキュメントなど)をApacheに提供したり、これらのURLを書き換えてMVCコマンドを実行したりするのに便利です。あなたがそう思うなら

、あなたの第五書き換えをカバーし、これを書くことができますようにあなたが持っている各controllerため

RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteRule ^/news$ ./index.php?news [L] 
RewriteRule ^/news/(.*)$ ./index.php?news/$1 [L] 

とします。数百のコントローラを用意する予定がある場合は、システムの設計を考え直すか、いくつかのメインコントローラのサブコントローラにする必要があると思いますので、このコントローラは.htaccessファイルに書き込むことができます。

また重要なのは、最終的に実行された行(条件と一致する場合は最後の行)をApacheに伝えるため、再帰を避けるためです。

RewriteRule ^/index.php?(.*)$ ./a/index.php?$1 [L] 

と最後を覆うように:

つのカバーするよう

RewriteRule ^/(.*)$ ./a/index.php?$1 [L] 

、書き換え1、2、3を覆います5と6を持つことができます:

RewriteEngine On 

# First rewrite 
RewriteCond %{HTTP_HOST} ^www.site.com$ [NC] 
RewriteRule ^(.*)$ http://site.com/$1 [R=301] 

# Second and fifth rewrite 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteRule ^/news$ ./index.php?news [L] 
RewriteRule ^/news/(.*)$ ./index.php?news/$1 [L] 

# Third rewrite 
RewriteRule ^/index.php?(.*)$ ./a/index.php?$1 [L] 

# Sixth rewrite 
RewriteRule ^/(.*)$ ./a/index.php?$1 [L] 
+0

私は自分の開発サーバーにアクセスできませんので、まだこれらのルールをテストすることはできませんでした。 –

+0

ありがとうございます!私はそれを少し修正して、今は素晴らしい作品です。 – user1373539