homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect bookmarks and links with mod_rewrite

10+ Year Member

Msg#: 3370 posted 7:54 am on Apr 19, 2005 (gmt 0)

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.



WebmasterWorld Senior Member 10+ Year Member

Msg#: 3370 posted 8:29 am on Apr 19, 2005 (gmt 0)

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://'


10+ Year Member

Msg#: 3370 posted 8:35 am on Apr 20, 2005 (gmt 0)

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..?


WebmasterWorld Senior Member 10+ Year Member

Msg#: 3370 posted 9:24 am on Apr 20, 2005 (gmt 0)

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.



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

Msg#: 3370 posted 5:23 pm on Apr 20, 2005 (gmt 0)

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?



10+ Year Member

Msg#: 3370 posted 5:17 am on Apr 22, 2005 (gmt 0)

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.


WebmasterWorld Senior Member 10+ Year Member

Msg#: 3370 posted 6:15 am on Apr 22, 2005 (gmt 0)


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.



10+ Year Member

Msg#: 3370 posted 3:47 am on Apr 24, 2005 (gmt 0)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved