Clyde Sanchez
Clyde Sanchez

Reputation: 41

How to redirect from a subdomain to a subfolder using .htaccess

I need help redirecting a subdomain to a subfolder on my website using the .htaccess file. I am not a pro in regards to configuring the .htaccess file, so please forgive me if my problem is a simple fix.

In my .htaccess file, I have the following code:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com  
RewriteRule ^(.*)$ /subfolder/$1 [R=301]

It almost works, but when I enter subdomain.domain.com into the address bar, I get redirected to subdomain.domain.com/subfolder/

This is really close, but I don't want the preceding subdomain in the redirected URL, I want the redirection to go to domain.com/subfolder/

Upvotes: 4

Views: 32499

Answers (3)

Syed Abidur Rahman
Syed Abidur Rahman

Reputation: 937

You may write in the following way:

RewriteCond %{HTTP_HOST} ^subdomain\.
RewriteRule ^(.*)$ http://%{HTTP_HOST}/subfolder/$1 [R=301,L]

Upvotes: 0

James Nguyen
James Nguyen

Reputation: 71

This will work for you:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^ http://domain.com/subfolder/$1 [L,R]

Upvotes: -1

animuson
animuson

Reputation: 54719

You need to change your RewriteRule to include the new domain you're trying to redirect too, otherwise it will treat the beginning / as a new path on the current domain.

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^(.*)$ http://domain.com/subfolder/$1 [R=301]

Should do the trick.

Upvotes: 14

Related Questions