iamgraeme
iamgraeme

Reputation: 450

.htaccess rewrite a url to a subdomain

I don't think this is possible, however I would like to ask the community to see if it is.

I have a blog on a subdomain blog.domain.com due to a revamp of the site we are having to use the blog on a trailing domain domain.com/blog this isnt ideal as all our old post permalinks point to the subdomain.

I was therefore wondering if there is a way to use the .htaccess to rewrite domain.com/blog -> blog.domain.com

Any help would be greatly appreciated.

Upvotes: 1

Views: 93

Answers (1)

anubhava
anubhava

Reputation: 786359

I believe you want to redirect all the traffic from blog.domain.com to domain.com/blog. Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(blog)\.(domain\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [NE,R=302,L]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Upvotes: 1

Related Questions