简体   繁体   中英

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

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]

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