Tharif
Tharif

Reputation: 13971

Redirect to subdomain with condition check

I have a domain :

example.com

with subdomain :

blog.example.com

Initially all the blogs where handled in main domain rather with subdomain.But now i have moved them to a folder "blog".

And earlier blog links where as :

example.com/123/blogcontent1
example.com/458/blogcontent2
.......
.......
example.com/458/blogcontentn

My question is how to write htaccess rule so that , whenever domain name / "numeric" appends redirect them to subdomain say as below example :

example.com/123/blogcontent1   ->  blog.example.com/123/blogcontent1
example.com/458/blogcontent2   ->  blog.example.com/458/blogcontent2

Also , I have already checked following posts , but they are not conditional ones as my problem stated ,

Redirect directory to a subdomain

How do I redirect domain to subdomain using htaccess?

It would be of great use if any help.Thanks

Upvotes: 1

Views: 181

Answers (1)

anubhava
anubhava

Reputation: 785481

You can use this rule in site root .htaccess of example.com:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^\d+/ http://blog.example.com%{REQUEST_URI} [NE,R=301,L]

Upvotes: 2

Related Questions