Reputation: 3
I'm trying to let the visitors of my subdomain only direclty in, when using a specific subdomain link format. I know that this would block SEs too, but I don't want the subdomain to be indexed anyway.
The allowed link should look like this:
subdomain.maindomain.com/aaa/bbb/ccc
and should be rewritten to this:
subdomain.maindomain.com/index.php?a=aaa&b=bbb&c=ccc
everthing what's not of this form and coming from an empty or external referrer shall go to the main domain and without the variables:
maindomain.com/
I tried with all kind of configuration, my last .htaccess file in the subdomain folder looks like this:
RewriteEngine on
RewriteRule ^/?(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [S=1,L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?maindomain\.com [NC]
RewriteRule ^(.*)$ http://domain\.com/ [L]
But it's still not doing what I want, it redirects also request for subdomain of the allowed form to the main domain and it also adds the vars as request to the main domain, going to this page
maindomain.com/?a=aaa&b=bbb&c=cee
Can you help me with the condition defined above.
And a second question, about the performance: I could obviously do this verification / redirect with PHP, what would be more efficient in your opinion?
Thank you!
Upvotes: 0
Views: 273
Reputation: 74128
You're pretty close. You need only a few additional parts
S=1
doesn't harm, but is not necessary either.index.php
from being redirected to the main domain.?
to prevent the arguments being forwarded to the main domain.R
flag, if you want the client to be redirected and not only the request rewritten.All parts together
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?maindomain\.com [NC]
RewriteRule ^(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [L]
RewriteCond %{REQUEST_URI} !$index\.php$
RewriteRule ^ http://domain\.com/? [R,L]
Regarding performance, if you rewrite or redirect with just .htaccess, the job is done before it even hits some PHP script. So, I think it is better to do this with .htaccess.
Upvotes: 0
Reputation: 4976
If I understood your logic correctly please try this:
RewriteEngine on
RewriteBase /
# Don't do any more rewrites if on index.php
# Note that you can add the HTTP_HOST condition here if you only want it to be active for the subdomain
RewriteRule ^index\.php$ - [L]
# If on subdomain then check that the referer is from main domain and attempt to match a regex
# If we find a match then ignore the next rule that rewrites subdomain to domain.com
# Basically this is like an awkward if-else statement..
# ================
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain\.com [NC]
# Rewrites /aaa/bbb/ccc to /index.php?a=aaa&b=bbb&c=ccc
RewriteRule ^(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [S=1,L]
# Redirect all requests from subdomain to domain.com by default
# ================
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
# Add the trailing question mark to delete all query parameters
RewriteRule .* http://domain.com/? [L]
Upvotes: 1