1

アプリケーションを構築していますが、サブドメイン「x」をマップしたいのですexample.com/_sub/x/が、フォルダー「x」がフォルダー「_sub」に存在する場合に限ります。そうでない場合は、example.comを表示してほしい。以下は機能しますが、ファイルまたはフォルダーがフォルダーx内にも存在する場合に限ります。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://example.com/_sub/%1/$1 [L]
</IfModule>

これは別の見方です。

doesNotExist.example.com -> example.com
test.example.com         -> test.example.com
example.com              -> example.com

これが私の更新されたコードです。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

#If the file and directory exists
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC]
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/ -d
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/$1 -f
RewriteRule ^(.*)$ http://example.com/_sub/%1/$1 [P]

#If the directory exists but the file does not
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC]
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/ -d
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/$1 !-f
RewriteRule ^(.*)$ http://example.com/_sub/%1/404.html [P]
</IfModule>
4

1 に答える 1

0

私が最初に抱えていた問題は、存在しないファイルをローカルの 404 ページに送信する 2 番目のルール セットを追加することで解決されました。これを理解すると、末尾のファイルのないリンクを除いてすべてが機能することがわかりました。[OR]これを修正するために、ステートメントと次の行を追加しただけです。うまくいけば、これは私の立場にある他の人の助けになります。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# If the file and directory exists
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC]
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/ -d
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/$1 -d
RewriteRule ^(.*)$ http://example.com/_sub/%1/$1 [P]

# If the directory exists but the file does not
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC]
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/ -d
RewriteCond %{DOCUMENT_ROOT}/_sub/%1/$1 !-f
RewriteRule ^(.*)$ http://example.com/_sub/%1/404.html [P]
</IfModule>
于 2013-03-08T19:45:08.890 に答える