简体   繁体   中英

Redirecting subdirectory to subdomain in .htaccess

I want to redirect a subdirectory to a subdomain using .htaccess . My current code:

<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>

This code redirects to sub.example.com/subdirectory . How do I remove /subdirectory from the new URL?

Place this rule on top, before all other rules:

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

Complete .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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM