Phil Meadows
Phil Meadows

Reputation: 23

.htaccess Redirect based on HTTP_REFERER

I'm trying to do what I've said above but I am getting a looping error and sometimes a 500 error!

What I want is for users requesting the root 'Home' page of the site to be redirected to /Welcome - UNLESS they are already browsing the site.

Here's my code:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^mydomain\.co\.uk$ [N]
RewriteCond %{REQUEST_URI} /index.php [N]
RewriteRule ^$ /Welcome [L]

Upvotes: 1

Views: 7624

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

You're probably getting the 500 error because the [N] isn't a valid RewriteCond flag, are you thinking of [NC] (no case)?

Additionally, you have a condition that directly contradicts the rule's match:

RewriteCond %{REQUEST_URI} /index.php

Says the URI must have a /index.php in it. While the rule itself:

RewriteRule ^$ /Welcome [L]

Says the URI must match ^$ which means it must be exactly /. And / will never be /index.php, so the rule is instantly invalid. Maybe you're thinking of either/or?

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^mydomain\.co\.uk$ [NC]
RewriteRule ^(index\.php)?$ /Welcome [L]

This will match the referer without case against mydomain.co.uk, and rewrite either / or /index.php to /Welcome

Upvotes: 2

Related Questions