web-dev-qa-db-ja.com

サブフォルダーは永続的にサブドメインにリダイレクトしません

現在、www以外のすべてのURLをwwwのURLにリダイレクトする次のコードがあります。

RewriteCond %{HTTP_Host} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

そのリダイレクトに加えて、/ dev /へのすべての呼び出しをdev.example.comにリダイレクトします。私はさまざまなアプローチを試しましたが、何も機能しませんでした(ReplaceMatchのように接近する少数のものはdev.example.comビットにリダイレクトし、残りをクエリ文字列として残します)。

何か案は?

編集:ルートフォルダーの完全な.htaccessを次に示します(プライバシー上の理由からいくつかを書き直しました)。

ErrorDocument 404 /404.html
AddType application/x-httpd-php .json
AddType application/x-httpd-php .xml

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# disables image hotlinking
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !google\. [NC] 
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

# Stop htaccess rules from accessing these areas of the website.
RewriteRule ^(banner|boxart|cgi-bin|content|forums|includes|newsimages|newsimg|sidebar|union-files|google5e645385256c3a63.html) - [L]

# Redirects /page URLs to /page/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R=301,L]

# Redirect old URLs to their new URLs
RewriteRule ^reviews/badurl--43.html$ http://www.gamingunion.net/reviews/goodurl--43.html [R=301,NC]

# Redirect non-WWW to WWW
RewriteCond %{HTTP_Host} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RedirectMatch 301 ^/dev/(.*)$ http://dev.example.com/$1

# URL Directory Redirects
RewriteRule ^([^/]*)/$ /index.php?p=$1&urltype=slash [L,QSA]
RewriteRule ^([^/]*)/$ /index.php?p=$1&urltype=slash [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?p=$1&lv1d=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?p=$1&lv1d=$2&lv2d=$3 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /index.php?p=$1&lv1d=$2&lv2d=$3&lv3d=$4 [L,QSA]

# URL File Redirects
RewriteRule ^([^/]*)\.html$ /index.php?p=$1&urltype=dot [L,QSA]
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?p=$1&lv1p=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&lv1d=$2&lv2p=$3 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&lv1d=$2&lv2d=$3&lv3p=$4 [L,QSA]

# For servers that support output compression, you should pick up a bit of speed but un-commenting the following lines.
php_flag zlib.output_compression On
php_value zlib.output_compression_level 5

/ dev /にあるものは基本的にこれと同じですが、上部にパスワード保護ビットがあり、サーバーのルートにのみ存在するいくつかのイメージフォルダーへのリダイレクトがいくつかあります。

4
Shawn Collier

..複数の.htaccessファイルがあります。1つはルートフォルダーに、もう1つは/devフォルダーにあります。

@Alexからルールをどこに配置しました-rootまたは/ dev .htaccess?おそらくルートにあります。それが機能しなかった理由です。

事柄は-Apacheが下位レベルのフォルダーに書き換えルールを見つけた場合、下位レベルの.htaccessにRewriteOptions inherit行がない限り、親フォルダーからルールを実行しません: http://httpd.Apache.org/ docs/current/mod/mod_rewrite.html#rewriteoptions

これで、次のコードが.htaccessに追加されました。

RedirectMatch 301 ^/dev/(.*)$ http://dev.example.com/$1

このディレクティブは別のモジュールに属し、とにかく実行されます..と実行されますaftermod_rewriteはすでにURLを書き換えています。そのため、リダイレクト後にrealURLが表示されます。

以下のルールが機能するはずです-それをあなたの/ dev/.htaccessに入れるだけです

RewriteCond %{HTTP_Host} !^dev\.example\.com
RewriteCond %{REQUEST_URI} ^/dev/
RewriteRule ^(.*)$ http://dev.example.com/$1 [R=301,L,QSA]

次の場合にリダイレクトするようApacheに指示します。

  • ドメインはdev.example.comではありません
  • URLは/dev/で始まります(メインドメイン経由でリクエストされた場合にのみ発生します)
7
LazyOne

これを試して:

RewriteCond %{HTTP_Host} ^(www\.)?example\.com$
RewriteRule ^dev/(.*) http://dev.example.com/$1 [R=301,L]
1
Alex

次のリダイレクトコードを使用して、サブドメインをURLパラメーターに書き換えました。

# rewrite subdomain
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_Host} ^([^\.]+)\.example\.org$ [NC]
RewriteRule ^(.*)$ index.php?q=116&company_name=%1$1 [L,QSA,NC]

%1は、サブドメインの正規表現一致です。 $1は、他のすべてのURLパラメーターで構成されます。

それをいじって、次のようにフォルダにリダイレクトします:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_Host} ^([^\.]+)\.example\.org$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L,QSA,NC]
0
feeela