user1675934
user1675934

Reputation: 1

How to .htaccess redirect domain to subdomain while keeping the main domain intact

I want to redirect all urls under my domain

domain.com/urls, www.domain.com/profile/user1.html etc. to subdomain.domain.com/urls and subdomain.domain.com/profile/user1.html etc.

but I dont want to redirect domain.com and www.domain.com to subdomain.domain.com

Is it possible via htaccess?

Edit: I want to redirect just the internal pages and files only. But leaving the main domain.com intact.

More Examples

domain.com/page1.html to subdomain.domain.com/page1.html

www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html

www.domain.com to www.domain.com (no redirection here)

Upvotes: 0

Views: 4299

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

You can first handle the main page by not rewriting and redirect everything else with a second rule

RewriteRule ^$ - [L]
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]

When everything works as you expect, you can change R to R=301.

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143856

Try:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]

If you want to permanently redirect, change the square brackets to: [L,R=301].

Upvotes: 3

Related Questions