I have a twiiter user linking to a specific page on my website which I would like to redirect to my home page. From the internet I have found and adapted the following code

RewriteCond %{HTTP_REFERER} (twitter.com) [NC]
RewriteRule myloginpage.php http://mywebsite.org/index.html [R,L,NC]

Unfortunately this doesn't do anything.

Can anyone tell me if there is an error in the code please?

Thank you

Recommended Answers

All 6 Replies

Pretty much every rewrite I have seen uses some form of regex to capture the condition. (twitter.com) will look for the exact string "(twitter.com)" as far as I understand it.

Maybe try using a simple regex to meet your needs -
RewriteCond %{HTTP_REFERER} .twitter.com.$ [NC]

Also, while I don't ultimately know the needs of your rule, Im guessing your RewriteRule will need to be modified some, as I think you are going to end up having a url that looks something like http://mywebsite.org/http://mysebsite.org/index.html assuming that the incoming uri is http://mywebsite.org/myloginpage.php

If that is the case, simply change
RewriteRule myloginpage.php http://mywebsite.org/index.html [R,L,NC]
RewriteRule ^(.*)$ http://mywebsite.org/index.html [R,L,NC]

and that may solve your issue...

So I re-read the above and the editor formatted something incorrectly above.

It should be:
RewriteCond %{HTTP_REFERER} *.twitter.com*.$ [NC]

Many thanks for the help with this, much appreciated.

Does the rewrite rule you've provided ^(.*)$ mean that any link from the twitter account will be redirected to my home page? That's not quite what I'm after, It is only links to the login.php page that I want to redirect, I'm happy with links to other pages

Yes, you are correct.

So instead, you could do something like
RewriteRule *.myloginpage.php$ http://www.mywebsite.org/index.html [R,L,NC]

Truth be told, I would have to tinker with it and figure it out myself, as I rarely have to do htaccess files.. you know.. do it once and you're usually done :-/

What I think the above will do is capture any URI that ends with myloginpage.php and redirect to your index.html instead.

Thanks again

My first attempt resulted in a 500 Internal Server Error but at least you've given me something to work on


I've had some success with the following

RewriteCond %{HTTP_REFERER} ^http://t.co/*$ [NC]
RewriteRule myloginpage.php$ http://mywebsite.org/index.html [R,L,NC]

Hoping to exclude twitter by using twitter.com was way off. They disguise their links as t.co

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.