Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess redirection.

htaccess redirect to multiple urls.



1:43 pm on Oct 22, 2010 (gmt 0)

Hey everyone.

New person, both here to the forum and to Apache.

I've been trying the last month to get a project done, but havent been able to find the correct way to do it.

So someone asked me, if i had tried this forum and decided to check it out. Basically here i am, in need of assistance.

The issue is:

I have a site domain.com/etc/, and i would like to figure out a way to redirect visitors, that has a specific referrer to a list of urls, randomly between urls.

So i was told that it might be possible to do with RewriteMap or RewriteCond. But im lost and i have absolutely no Apache coding experience.

i've created this picture, to show how i would like it to work: [myupload.dk...]

(please disregard the iframe and php pieces.)

Hopefully you guys can help out. I would really appreciate this, if you could. :)

Thanx in advance.


2:24 pm on Oct 22, 2010 (gmt 0)

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

Hi Soren, and welcome to WebmasterWorld!

If your list of specific referrers is short, then using a "lookup table" made using RewriteConds is feasible. Otherwise, use a RewriteMap (requires server admin privileges to define, but not to use, the map).

But be aware that using HTTP Referer header values is inherently unreliable. The HTTP Referer header is optional and is also easy to spoof.

Visitors from certain corporations and ISPs (AOL and EarthLink is the U.S., for example) will connect to your site through caching proxies, which are used to reduce the traffic in and out of their networks. These users are usually not aware of this network function. These proxies effectively remove the HTTP Referer header from requests, because one request to your server may represent many requests from their users.

In addition, some visitors will be using "internet security" software or browser plug-ins which suppress the HTTP Referer header. And again, these visitors may or may not be aware of this fact.

And finally, requests from multimedia plug-ins, media players, and links coded in JavaScript will not be sent with an HTTP Referer header.

All of these can make such referrer-based functions unreliable or even useless. In those cases, setting and testing a client-side cookie to determine one visitor from another is often a much better approach.

This is a "guided self-help" forum. Please see our Apache Forum Charter (link at top of this page) for more information on getting the most from this forum.



2:31 pm on Oct 22, 2010 (gmt 0)

10+ Year Member

Hi SorenC -

Welcome to WebmasterWorld -- you're in the right place, but there are some rules and guidelines that will help you get your questions answered quickly and accurately. At the top of the page, above forum name, there's a link to Charter and Library. Read the charter, and browse the library for some really good introductions to using Apache rewriting -- you'll probably need to do the work to understand this in order to get the problem resolved.

The problem of how to identify a specific referer (correctly spelled "referrer", but misspelled since HTTP was invented) is straightforward: use a RewriteCond to match the header %{HTTP_REFERER} against a pattern. This starts the rewrite process.

The problem of how to randomly redirect to one of a few URLs is harder. If it really has to be random, then you might have to write a PHP program to handle this case. There may be things you can to to check against something that varies, but isn't really random, for example the last digit of the IP address (in the header %{REMOTE_ADDR}) such that you can switch between URLs.

There are other solutions that you can do with rewriting, but let's see your best attempt at creating the commands needed in .htaccess to identify the referer you care about and send it to one particular URL and take it from there.



6:18 pm on Oct 22, 2010 (gmt 0)

Hey both of you.

I've figured as much, as to get the referrer with %{HTTP_REFERER}.

But if im supposed to use a php script to handle the randomization of the urls, the htaccess redirects to, how do i get it to activate the php file?.

I've been reading up alot of the stuff in here, but i get confused very quickly. :/


7:05 pm on Oct 22, 2010 (gmt 0)

10+ Year Member

Could you provide a more specific example? Are there three files you want to alternate between? 1000? Do they have a predictable filename/URI? Does it have to be truly random or is "sort of random" OK.

And what have you tried so far?



7:31 pm on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

You internally rewrite the URL request to the PHP file, and the PHP file sends the external redirect response back to the browser.


12:57 am on Oct 23, 2010 (gmt 0)


Well i have a list of 5 urls, that i want the visitors to be randomly distributed between. Thats basically what i want.

g1smd, could you provide an example of that?.


1:36 am on Oct 23, 2010 (gmt 0)

10+ Year Member

SorenC --

Please refer to a comment from the moderator
This is a "guided self-help" forum. Please see our Apache Forum Charter (link at top of this page) for more information on getting the most from this forum.

You answered 2 of my 6 questions. I am not trying to be a pain, but just to say that this is a place where we try to help people isolate unexpected behavior and learn, rather than guessing at an answer, since in many cases the guess is wrong and misleading and all others who might be reading will come away with a bad answer.

By "example" you might provide something like:

When the referer contains "example.com" I want to give them one of five html pages that I internally call "spoof1.html". "spoof2.html" ... and so on. The goal is to prevent a sleazy site from stealing my content, and also from knowing that I am telling their users that the sleazy site is a lying, stealing, cheating site.

Here's what I have tried so far...

If you really need random, as far as the PHP, check out the rand() function which can return an integer, and the header() function, which can be used to set the HTTP status and new location.

Google "php 301 redirect" and "php rand" for examples. Google "apache 2.2 mod_rewrite" for more info on rewriting.

Then ... let us know what has worked, what has not, how you did it, and so on.



11:22 am on Oct 23, 2010 (gmt 0)


I actually did put a picture up, that states, exactly what i would like it to do.

I have already been looking around the charter and library, but its very confusing, when im all new to apache.

Basically i want to have visitors divided.

Visitors from a specific (only 1) referrer should be redirected to a list of urls, that gets randomly shown. Like a rotator of sorts.

Visitors, that are not from this referrer sees the site normally.

Previously, i've tried to figure out how to get a php file activated/run by the htaccess file, but that hasnt been successful, since i dont really know how to do it.

Im sorry, if i havent answered all your questions, but in my defence. I did post a picture, that explained it all basically.


5:13 pm on Oct 23, 2010 (gmt 0)

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

The third post in this thread explains how to do this. In conjunction with the documentation cited in our Charter, it should guide you to a solution -- or at least close enough that we can productively help you. But make no mistake, this is server configuration code here, and if you do not understand it, it can put you out of business overnight.

Please make an effort to study the mod_rewrite documentation and other resources cited in our Charter, and take a look through our Apache Forum Library for examples. If you don't have time or do not wish to learn to do this sort of coding, then hiring a local consultant to work closely with you would be a more sensible approach.

Here are the two most-basic forms of RewriteRules:

# External URL-to-URL redirect
RewriteRule ^requested-local-url-path$ http://hostname/new-URL [R=301,L]
# Internal URL-to-filepath rewrite
RewriteRule ^requested-local-url-path$ /internal-filepath [L]

To invoke a script, use the second form.



9:53 pm on Oct 23, 2010 (gmt 0)


As i said earlier. I wasnt much into learning the code, but just to see if i could get a quick answer, that could help me in the right direction.

I get confused when i have to start out with a completely new "language" and thus, i got confused, trying to navigate in the charter/library.

But im guessing that i wont get the assistance, that i needed in here.

So perhaps i'd be better off getting the help from another place.

Though i would like to thank you 3 for answering.


11:29 pm on Oct 23, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

The question you have asked, has already been asked and answered hundreds of times before in the same or a similar form, and the resources cited will get you a long way down the road to a solution.

You are dealing with server configuration code, code that can break your site and put you out of business if you get it wrong, so it is vital that you understand every character, every digit and all the punctuation in the code you install on your server.

The problem with this forum is that the number of people asking questions is high, and the number of people available to offer a free "write your code for you" service is low - so an alternative approach is called for. That includes education about the methods and syntax, rather than a free code writing service.

Specific questions are welcome and are often discussed in depth - as the previous seventy thousand threads amply demonstrate: hundreds of which contain the code you require with detailed notes as to the design logic and correct syntax to use.

If you don't want to take the time to learn how mod_rewrite operates then it is highly suggested that you do not make these changes to your server. In an emergency, you would have no way to maintain or fix the code; and while your site is offline you'd be losing money.

Featured Threads

Hot Threads This Week

Hot Threads This Month