Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect bookmarks and links with mod_rewrite



7:54 am on Apr 19, 2005 (gmt 0)

10+ Year Member

I was wondering if there was a way to redirect links to my site with session IDs to a url without a session ID using mod_rewrite.. So that:


redirects to:


if and only if the referer does not contain "mydomain.com"

Thanks in advance for your help.


8:29 am on Apr 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Hi webapp,

This should do the trick, or get you close for any browser that sends referer headers. (You probably do not want to redirect blank headers, or someone from your site will keep getting redirected... If you do, remove the first condition. Not Recommended.)

RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www.)?yoursite.com [NC]
RewriteRule ^cgi-bin/shop\.pl/SID=12345\.1234/page=item\.html$ http:// yoursite.com/cgi-bin/shop.pl/page=item.html [L]

Condition one checks to make sure the header is not blank.
Condition two checks to see if the referer is not from your site.
If the header is not blank and the header is not from yoursite the conditions = true, and the rule is invoked.
The? makes www. optional, if you don't use www, you can remove the (www.)? protion of the code.

Please, keep in mind this is only for exactly the page you refered to in your question, but can be globalized through the use of regular expressions.


BTW make sure there is a space before the '!', and no space after the 'http://'


8:35 am on Apr 20, 2005 (gmt 0)

10+ Year Member

Thanks for your help. Can't seem to get it to work, though. For some reason, the SID is always intact; I'm guessing the rewrite conditions aren't being met..?


9:24 am on Apr 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

It could fail for any or all of these reasons:

1. The browser you are using is not passing referer information.

2. The URL is not presented to the .htaccess file the way it appears in the browser. EG Another rule is involved, or PHP is modifying the headers.

3. The information after the SID is not being passed as an html page, as it appears, but rather as a query string.

4. The .htaccess file is not in the correct location to 'catch' the rule in the exact way it is written.

You can try changing browsers, and make sure you are not running any software that would inhibit the passing of referer information.

Double check the location of the .htaccess file.

Make sure there are no other rules, conditions or files affecting the URL.

If for some reason the URL is not passed as it appears try moving the Rule to a third condition of {QUERY_STRING} (Check this forum or Apache for correct query string formatting). Then rewrite all url's that pass all 3 conditions to your new location.

Remember, it has to be exact to your situation. People here can get you close and point you in a direction, but most of the time you will have to 'adjust' to exactly what you need.

If follow up on these and still can't get it to work, keep posting.

Hope this helps.



5:23 pm on Apr 20, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Same code with a few tweaks:

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yoursite\.com [NC]
RewriteRule ^cgi-bin/shop\.pl/SID=12345\.1234/page=(.+)\.html$ http://yoursite.com/cgi-bin/shop.pl/page=$1.html [R=301,L]

Not mentioned above, but important to success:
Is this code for use in .htaccess or in httpd.conf?
Do you have "RewriteEngine on" and "Options +FollowSymLinks" in that file already?



5:17 am on Apr 22, 2005 (gmt 0)

10+ Year Member

Thanks to the both of you. I got it to work with your help. I changed it up a little:

RewriteCond %{HTTP_REFERER} !^http://mysite\.com [NC]

RewriteRule ^cgi-bin/shop\.pl/SID=(.+)/page=(.+)\.html$ http://mysite.com/cgi-bin/shop.pl/page=$2.html [R=301,L]

Since I use this to remove the "www" from my domain:

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]

RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]

I removed the "(www\.)?" from the second condition posted in both your suggestions.

I also removed the first condition from both jd01's and jdMorgan's code. I didn't want people to copy and paste the URL and have the SID still there. I've tested it and it seems to work fine; once users enter my site, the ID remains throughout the session. I wasn't able to create an instance where a user browsing my site would keep getting redirected, as jd01 mentioned.. Please let me know if I'm missing any situations where this might happen.

Thanks again to you both. Now I can have people link to my site with SIDs without having to worry about cart content mix ups.


6:15 am on Apr 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


It's great when they work, isn't it?

The instance I was refering to where a user could get bounced would be if they had their browser (or I believe some proxy connections) set to not send referer headers. EG if they run some type of "I don't want you to see who I am, or where I came from," foil hat software.

It may not be an issue for you, or you might want to put a warning on the site to those who do.

As an aside: It does not sound like a page that needs to be indexed, but search robots do not send referer headers either, so on a page that needs indexing, it is essential to allow those who do not send headers full access.



3:47 am on Apr 24, 2005 (gmt 0)

10+ Year Member

Yeah, I do (rarely) have people that come to my site with referrals off. I've added a blurb in my FAQ section. I think the search engines should be fine since they won't need SIDs. Thanks for the info.

Featured Threads

Hot Threads This Week

Hot Threads This Month