linstar
linstar

Reputation: 143

Redirect from one directory to a subdomain using .htaccess

I want to redirect all the urls of a directory (example.com/blog/urlnames) on one domain to a sub-domain (blog.example.com/urlnames)

I tried:

RewriteEngine On  
RewriteBase /  
RewriteCond %{HTTP_HOST} ^/blog$ [NC]  
RewriteRule (.*) https://blog.example.com/$1 [R=301,L]

I keep getting a 404 error when I type the url on example.com:
https://example.com/blog/5-names-for-actors/

I want this to go to:
https://blog.example.com/5-names-for-actors/

Upvotes: 1

Views: 22

Answers (1)

anubhava
anubhava

Reputation: 784898

You can use this rule in blog/.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC] 
RewriteRule (.*) https://blog.example.com/$1 [R=301,L,NE]

File blog/.htaccess may not be existing already so you may have to create it.

Upvotes: 1

Related Questions