Forum Moderators: phranque

Message Too Old, No Replies

More Efficient Rewrite

rewrite requiring user to visit main site to access subdomain

         

MickeyRoush

4:19 am on May 3, 2011 (gmt 0)

10+ Year Member



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]

lucy24

4:36 am on May 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



example\.(com|net)/

or

example\.\w\w\w/

g1smd

7:32 am on May 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure you cater for blank referrer as otherwise you will block
- all visitors using Internet Security Software that strips referrer data from their requests, and
- all visitors from large ISPs that happen to use a caching proxy (AOL and Earthlink among others).

MickeyRoush

8:05 am on May 3, 2011 (gmt 0)

10+ Year Member



@lucy24
Thanks, that worked!

@g1smd
Thanks, I didn't think about that. I'm assuming that's along a similar route with mobile users.

Would this work to help cater for a blank referrer?:
RewriteCond %{HTTP_USER_AGENT} ^$

If I put it right before:
RewriteRule ^(.*)$ http://example.com/page1 [L]

Would I also need to include the flag [OR] somewhere?


Again, thanks for the quick replies.

g1smd

8:08 am on May 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need the standard AND function, not OR. Think about it.

Yes, ^$ (nothing) or !. (not something) will do it.

The ^(.*)$ pattern doesn't need to be captured as a backreference, so just use .* here.

Your rule sends a 302 redirect to a different page, doubling the work of your server. I'd just use
RewriteRule - [F]
or similar.

lucy24

10:23 pm on May 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure you cater for blank referrer as otherwise you will block
- all visitors using Internet Security Software that strips referrer data from their requests, and
- all visitors from large ISPs that happen to use a caching proxy (AOL and Earthlink among others).

- yourself (when visiting from your home computer) :)