Home > database >  .htaccess redirect a folder in subdomain to a folder in main domain
.htaccess redirect a folder in subdomain to a folder in main domain

Time:12-19

Here's what I need.

I need to redirect (301) all content of blog.example.com/sample/ to example.com/sample/ and I'd like to accomplish this using htaccess.

Here's what I tried, but it didn't work:

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

Thanks for the help.

CodePudding user response:

RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]

Here having /sample is problematic because HTTP_HOST only matches host name part in a request so it will never match this condition.

You can use this rule instead:

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