M.Parent
M.Parent

Reputation: 183

htaccess serve page from another subdomain silently only when page is not found

My main pages are at "main.mysite.com". Customer access site by "customer.mysite.com" which contains only a subset of the main pages. When customer request page "customer.mysite.com/data.php", I want to check first if the file is in "customer.mysite.com" subdomain, if yes, then serve that page, if not, then serve the page at "main.mysite.com/data.php" subdomain. I also want to keep the url at "customer.mysite.com/data.php" for the two cases.

My complete htaccess file is currently :

# This will enable the Rewrite capabilities
RewriteEngine On
RewriteBase /

# This rule will convert request urls like  /category/page?id=1  to  /?c=category&p=page&id=1
# Redirect to main page, which is Single Page Application and then manage to open the new tab
RewriteRule   ^([A-Za-z]*)\/([A-Za-z]*)([?]?[A-Za-z0-9=&]*)$   /?c=$1&p=$2   [NC,R,QSA]    

# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
# First, this checks to make sure the connection is not already HTTPS
RewriteCond   %{HTTPS}   !=on
RewriteRule   ^/?(.*)   https://%{SERVER_NAME}/$1   [NC] 

# This rule will serve pages from  main.mysite.com  when browsed with  customer.mysite.com
# By removing the [R=301], it makes an internal redirect, keeping the original url in the browser
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ https://main.mysite.com/$1 [L,NC,QSA]

# Disable Directory Listing
Options -Indexes

<Files .htaccess>   
order allow,deny    
deny from all   
</Files>    

However, when I browse "customer.mysite.com/page.php", I am redirected to "main.mysite.com/page.php", which is not what I want.

Upvotes: 0

Views: 71

Answers (2)

M.Parent
M.Parent

Reputation: 183

By adding the proxy flag [P] to the rule, the server makes an internal redirect, keeping the browser url unchanged. Normally, this would work by not specifying the R=301 flag, but it'S not enough when the rule is changing domain/subdomain.

What worked:

RewriteRule ^(.*)$ https://main.mysite.com/$1 [P,L,NC,QSA]

Note that the L flag is not required with P as it is added implicitely, as no other rules can be executed after that.

Upvotes: 0

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

First , to redirect /category/page?id=1 to /?c=category&p=page&id=1 :

RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule   ^([A-Za-z]+)\/([A-Za-z]+)$   /?c=$1&p=$2   [NC,R=301,QSA] 

change this : RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [NC]

to this RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

Because %{HTTP_HOST} it is request header including your target host

Moreover :

I f you handled error file like that , when there is no file in that target looping will happen so it is better to handle that like this :

replace this :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ https://main.mysite.com/$1 [L,NC,QSA]

With this :

RewriteCond %{HTTP_HOST} !^(www\.)?main.mysite.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ https://main.mysite.com/$1 [L,NC,QSA]
#then you could handle the error that not found in main by this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /path/to/error/page [L]

Upvotes: 2

Related Questions