bklups
bklups

Reputation: 117

Apache redirection of subdomain

I have a subdomain (called subdomain.mydomain.com)

I just want that a page (ex: www.mydomain.com/page1.php) redirects to my subdomain

So when i load subdomain.mydomain.com i want to go to www.mydomain.com/page1.php but conserving my subdomain in the url

Using htacess or virtualhost

here is my edited code with your comments

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{QUERY_STRING} (^|&)com=who(&|$) [NC]
RewriteCond %{HTTP_HOST} ^www\.mysite\.fr$ [NC]
RewriteRule ^index\.php/?$ http://apropos.mysite.fr/? [NC,L,R]

RewriteCond %{HTTP_HOST} ^apropos\.mysite\.fr$ [NC]
RewriteRule ^$ http://www.mysite.fr/index.php?com=who [L,P,QSA]

Upvotes: 1

Views: 495

Answers (2)

anubhava
anubhava

Reputation: 785541

You can try following code in your ROOT .htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine on

# Redirects www.mydomain.com/page1.php?com=who to subdomain.mydomain.com
RewriteCond %{QUERY_STRING} (^|&)com=who(&|$) [NC]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^page\.php/?$ http://subdomain.mydomain.com/? [NC,L,R]

# Internal Redirect(Proxy) subdomain.mydomain.com to www.mydomain.com/page1.php?com=who
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^$ http://www.mydomain.com/page.php?com=who [L,P,QSA]

Upvotes: 1

Ulrich Palha
Ulrich Palha

Reputation: 9539

So when i load subdomain.mydomain.com i want to go to www.mydomain.com/page1.php but conserving my subdomain in the url

Try putting the following in the .htaccess in the root directory of you site. I am assuming that your subdomain and domain share the same root.

RewriteEngine on
RewriteBase /

# page (ex: www.mydomain.com/page1.php) redirects to my subdomain
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
#do not redirect if from proxy
RewriteCond %{QUERY_STRING} !(^|&)r=1(&|$) [NC]
RewriteRule ^page1\.php/?$ http://subdomain.mydomain.com [NC,L,R=301]


#if subdomain.mydomain.com
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
#and root directory, then proxy to page1
RewriteRule ^$ http://www.mydomain.com/page1.php?r=1 [P,L]

Upvotes: 1

Related Questions