Forum Moderators: phranque

Message Too Old, No Replies

Re-directing using .htaccess

query on given code

         

Brutus14

4:45 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



A couple of years ago I sought advice through this site. I was looking for advice on how to stop all referrals to pages within my site from referrers outside my site.

I received two alternatives which I show below.

# Block foreign referrers
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://www\.yoursite/ [NC]
RewriteRule .* - [F]

and

SetEnvIf Referer "^http://www\.mysite\.com" allowit
SetEnvIf Referer "^$" allowit
<Limit GET>
Order Deny,Allow
Deny from all
Allow from allowit
</Limit>

Both alternatives deny access with a 403 Forbidden message. Is there a chance that one of them could be re-written to take the visitors to my index page instead of the Forbidden page.

I am a complete novice so any sound advice would be appreciated.

Brutus

ChadSEO

4:54 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Brutus,

Welcome back to WebmasterWorld. The first one could easily be rewritten to redirect them to a different page:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www\.yoursite/ [NC]
RewriteRule .* http://www.yoursite.com/somepage.htm [R,L]

[edited by: jdMorgan at 5:22 pm (utc) on Aug. 4, 2005]
[edit reason] Fixed [ code ] formatting. [/edit]

Brutus14

5:19 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Thanks for the reply ChadSEO.

Sorry but that gave an Internal server Error

Brutus14

ChadSEO

5:35 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Brutus,

Try the code again, now that Jim has cleaned it up. I just tested it on my site, and it didn't give me any errors.

Brutus14

5:53 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Hi Again ChadSEO

Again thanks for the reply.

It does not give any errors now, but unfortunately it does not re-direct either. Outside links still work.

Brutus

jdMorgan

6:46 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Flush your browser cache before testing any changes to access-control code.

Jim

Brutus14

7:01 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Thanks for the reply Jim.

I flushed the cache as you suggested. Now the links will not load at all. The indicator in the "start bar" at the bottom of the page fills from left to right very slowly (it took 3 minutes) when it eventually reached the right hand side of the indicator it just stopped there. The browser refuses to load a page. I tried this on both my machines, same result i'm afraid.

Brutus

ChadSEO

7:27 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Looks like there's an infinite redirect there, I just Actually tested it on my site. The problem appears to be that when redirecting, the referer does not change (you learn something everyday!). I was able to fix it by using the following code:

RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://www\.example\.com/ [NC]
RewriteCond %{REQUEST_URI}!^/index\.html
RewriteRule .* http://www.example.com/index.html [R,L]

I don't know if there's a better way to fix it, but that's one way.

Brutus14

7:52 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Sorry but still no go. The last version gave an Internal server error and denied access to the index.html page.

Fortunately I was already in Cpanel on one of my machines so it did not cause a problem

ChadSEO

7:59 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Brutus,

Sorry, it looks like it's still doing funny things with the formatting. First off, you may, need the "Options +FollowSymLinks" line at the beginning, which I left out. Also, make sure there is a space between "%{HTTP_REFERER}" OR "${REQUEST_URI}" and the exclamation mark (!).

Brutus14

8:13 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



No luck i'm afraid.

The directive now has no effect at all. All pages load immediately.

Brutus

ChadSEO

9:04 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Brutus,

Just to make sure, you are attempting to access the URLs from an outside referer, and they still load? This should work in that, if you type in a URL, it will work. If you click a link from your website, it will work. If you click a link from another site, it will redirect to whateverpageyouchose.html.

Brutus14

9:15 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Yes I understand what you are saying, but it is not happening i'm afraid.

I am attempting to access these files from the offending site. I have flushed my cache before each attempt, and the links load quite normally. If I go back to the original files at the beginning of this thread I get the 403 Forbidden page.

Brutus

ChadSEO

9:33 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Well, I have to confess, I'm stumped. Perhaps someone else has a suggestion (*cough* Jim *cough*).

I might be helpful if you pasted your .htaccess code ( with examplified URLs, of course), maybe someone will catching something.

Brutus14

9:41 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Good news,

I just deleted the .htaccess file, entered the code again, rebooted, cleared the cache, and BINGO it works.

I really must thank you for your expertise and patience in this matter.

Its nice to know there are still some good guys out there.

Brutus

ChadSEO

9:47 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Glad to hear it Brutus, sorry it took so long :) Definitely come on back when the next issue come up.

Brutus14

10:12 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Rest assured, I now know where to turn for help and advice.

Once again, many many thanks

Brutus

Brutus14

5:58 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



ChadSEO

I have discovered a couple of problems with the code, which, I must hasten to add, works perfectly.

I have a forum on my site, and a members area. I have just discovered that in both cases all login attempts (which require username and password) are redirected to the index page as soon as "Login" is clicked. Can anything be done?

Brutus14