将子目录重定向到.htaccess中的子域

时间:2013-10-24 12:39:38

标签: regex apache .htaccess mod-rewrite subdomain

我想使用.htaccess将子目录重定向到子域。我目前的代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?sub.example.com$
RewriteRule ^(/)?$ subdirectory [L]
</IfModule>

此代码重定向到sub.example.com/subdirectory。如何从新网址中删除/subdirectory

1 个答案:

答案 0 :(得分:1)

在所有其他规则之前将此规则放在首位:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^subdirectory/(.*)$ http://www.sub.example.com/$1 [L,NC,R=301]

完成.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^subdirectory/(.*)$ http://sub.example.com/$1 [L,NC,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?sub\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !(jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteRule !subdirectory subdirectory%{REQUEST_URI} [NC,L]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>