I’m a newbie when it comes to htaccess. What I’m using works, but I was wondering if there’s a better or more efficient way to do what I’ve done.
I have a site where there are several pages in iframes. Those pages that are in iframes are on subdomains of the main site. Example: example.com is the main site, page1.example.com/ is page 1, page2.example.com is page 2, etc.
They are displayed via iframes. But if you were to control click a link on that page, that would allow you to actually view the subdomain in a new window or tab without viewing or even going to the main site. So I set up these rules on the subdomains requiring you to go to the main site first, otherwise you’ll be sent to the main site. So even if they bookmark the actually subdomain’s url, then try to access it without going to the main site, they’ll be sent back to the main site.
This example is only for the first subdomain, but I have similar ones for the other subdomains, changing page1 to page2, etc. So if they try to go directly to page1.example.com they’ll be sent to that page on the main site which would be example.com/page1.
# Only allow access to this page via main site first
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example.net/ [NC]
RewriteRule ^(.*)$ http://example.com/page1 [L]
This works, but is there anyway to make this more efficient? Could I combine the .net and .com address on one line using syntax like (example.com|example.net)?
So:
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?(example.com/|example.net/) [NC]