FamousWolluf
FamousWolluf

Reputation: 578

Rewrite subdomain to main domain without redirect with htaccess

Hello I want to rewrite sub.domain.com to domain.com/sub.
But also sub2.domain.com needs to be rewritten to domain.com/sub2 and sub3 etc...
The url is not allowed to change so a redirect is not an option.
My current code is an endless redirect:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^(.*?)/?$ http://sub.domain.com/$1 [L]

How can I change the .htaccess to get this fixed.

Upvotes: 1

Views: 1982

Answers (1)

arco444
arco444

Reputation: 22831

The loop happens because you rewrite to sub.domain.com which then triggers the cond because of the presence of sub. and rewrites again.

If you don't want the address to change, you can try using the proxy flag instead. Make sure mod_proxy is enabled:

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^(.*)/?$ http://domain.com/%1/$1 [P]

Upvotes: 3

Related Questions