Forum Moderators: phranque

Message Too Old, No Replies

Redirecting specific pages in subdomain

         

CrashGordon

12:34 am on Sep 7, 2012 (gmt 0)

10+ Year Member



My apologies if this seems too simple to have a problem with, but I'm new to doing this type of thing.

I need to redirect requests to a subdomain to an entrance page in the parent domain. I've succeeded in doing that with the following.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example\.com/ [NC]
RewriteRule ^$ http://forum.example.com/redirect.php [R]


However if someone appends "/index.php" or "/portal.php" to the subdomain, those requests are not redirected. I need to close that avenue of access, but everything I've tried, doesn't work.

I recognize that my ignorance is showing, but it someone could please let me know how this is done, I'd really appreciate it.

TIA

lucy24

5:13 am on Sep 7, 2012 (gmt 0)

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



However if someone appends "/index.php" or "/portal.php" to the subdomain, those requests are not redirected.

Good. That lets you kill two birds with one stone. Requests for "index.php" by name should in any case be redirected to the directory's proper name, which ends in /. (Redirect only the index filenames that you actually use-- php OR html OR htm or whatever it may be. The others can jolly well get a 404. They've earned it.)

So, as usual, you go from most specific to less specific to final mopping-up. The pattern is:

First:

RewriteCond looking at the desired subdomain
RewriteCond looking at THE_REQUEST for index.whatever, so you're only redirecting people who asked for the index file by name.
RewriteRule that redirects index.php to http: {et cetera} redirect.php

Second:
RewriteCond looking at THE_REQUEST for index.whatever, as above
RewriteRule that redirects anything else in index.php to http://www.example.com/{directory name, if any}

Every rule needs an [L] flag meaning "If you've applied this Rule, don't go looking for any others". Every redirect needs an [R=301] flag to make it a permanent redirect. The [R] by itself is pointless because giving the full protocol-plus-domain (which is the correct thing to do) has already turned your rewrite into a redirect-- but it defaults to 302. You rarely want that.

Gotta say I don't understand the reference to a Referer in your quoted rule. Where is the subdomain referenced?

CrashGordon

5:44 am on Sep 7, 2012 (gmt 0)

10+ Year Member



Thank you for the response. I'll try to clarify. The quoted rule is in the .htaccess in the subdomain and is (hopefully) designed to trigger the redirection of any request not bearing the HTTP_REFERRER of the parent domain. That works as desired. It is only if the request includes /index.php (or any other page reference) that it fails. I only need them redirected if the referrer is not the parent domain, because I have links on the parent home page to specific pages in the subdomain. I hope I clarified that rather than making it more difficult to understand.

But, you've given me some clues, so after I've had a cup of coffee, tomorrow morning, I'll try to.

g1smd

6:54 am on Sep 7, 2012 (gmt 0)

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



^$ matches only "/" - rule patterns are localised on a "per directory" basis.

If you need to redirect other requests, alter this pattern to match those too.

Don't forget that Googlebot won't send a referrer, so it will be redirected too.

CrashGordon

3:35 pm on Sep 7, 2012 (gmt 0)

10+ Year Member



I should be able to give the bots the full path to follow, in a manner that would not be easily discerned by human visitors. such as a 1x1 transparent gif linked to http://example.com/directory1/directory2/index.php. The greater concern is that those viewing the site are getting the same results when they perform the same action.

CrashGordon

8:16 pm on Sep 7, 2012 (gmt 0)

10+ Year Member



Okay, I think I owe those who provided advice an explanation of why I wasn't making much progress. To my chagrine, I learned it is not a good idea to test site settings, using IE9. Ctrl-F5 doesn't always clear its cache. I discovered this when testing settings from my home page and also accessing the subdomain directly, and not getting the expected results. Then I started experiencing requests from the home page being redirected because HTTP_REFERRER data was being provided by IE9 that I was accessing the page form the subdomain, rather than the parent domain

Needless to say, I'm using a different browser, now, to test things.

g1smd

9:18 pm on Sep 7, 2012 (gmt 0)

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



You need the Live HTTP Headers extension for Firefox to really see what is going on.

Running a plain-text list of test URLs through Xenu Linksleuth is also a very good idea.