2

Is it possible to use mod_rewrite to force all users entering a site (either through a link from another site, or by typing a URL in the address bar) to be redirected to the homepage? From the homepage (or any page within the site), users should then be able to access other pages in the site. But all users would be forced to enter the site through the homepage. Can this be done with mod_rewrite (or without using a scripting language)?

2 Answers 2

4

Sure, with a few possible drawbacks. The simple version looks like this:

RewriteCond %{HTTP_REFERER} !example.com
RewriteRule ^/. / [R]

This checks that the referrer does not contain your domain name and then for any URI that has something after the first slash, (i.e., any page other than the home page) sends the user a redirect to the home page.

This will break your site completely for any user that doesn't send referrers or modifies the referrers. For instance, some privacy applications change referrers to http://xxxxxxxxxxxxxxxxxxxxxxx where the number of xs matches the length of the original true referrer.

If you send an email with an image hotlinked from your site, the above rule will break that too.

It is also bad usability for visitors that click on a link to a specific page from another site (such as Google). They are then redirected to the home page and have no idea how to get back to the page they were on. These visitors are likely to hit their back button and choose a different search result. You could make this a little better by changing the rewrite rule.

RewriteCond %{HTTP_REFERER} !example.com
RewriteRule ^(/.+) /?prev=$1 [R]

You can then use your app on the home page to provide a link back to the page they were on. You can also set cookies with mod_rewrite so that ?prev=$1 could exist inside a cookie instead.

There are probably other usability drawbacks that I have not foreseen. You should keep a close eye on your visitors if you implement this.

I just thought of another drawback: Bots rarely send referrers. If you implement this as written and don't maintain a complete list of desirable bots (User-Agents and/or IP ranges) and disable it for them then your site will be uncrawlable by search engine bots.

2
  • Excellent response--thank you very much. You thought of some usability items that I had not (particularly the bot search issue).
    – scotru
    Jun 8, 2012 at 4:33
  • Couldn't get this code to work. Ended up using: RewriteEngine on RewriteBase / RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC] RewriteRule ^(.*)$ http://example.com/ [R]
    – scotru
    Jun 16, 2012 at 7:17
1

You could use PHP:

First that comes up to my mind is the use of "$_SERVER['HTTP_REFERER']" however, this can be disabled by the user on the client side if that is shown or not as far as I am concerned, please correct me if I am wrong. So you could do something like Pseudo code:

if $myDomain is part of $_SERVER['HTTP_REFERER'] then
redirect
else
do nothing

Since people could change that, you could introduce a session variable in PHP. Pseudo code:

if empty( $_SESSION['newvisitor'] ) && !isset($_SESSION['newvisitor']) 
{
    $_SESSION['newvisitor'] = true;
}
else
{
   $_SESSION['newvisitor'] = false
}

if ( $_SESSION['newvisitor'] )
{
    #redirect to home
}

So basically the script checks in the beginning if the variable is set, if it is not set than it must be new visitor, therefore it should redirect to the homepage. If that is actually set then it should not redirect.

EDIT: Recognised, my first solution was wrong since it could not match if he actually came from a subpage.

1
  • Thanks Richard--I'm trying to avoid needing to use a server-side scripting language as this is a static HTML site. I'm also trying to avoid a dependency on cookies if possible. I don't want users to be able to disable it from the client side.
    – scotru
    Jun 8, 2012 at 4:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .