Forum Moderators: phranque
My question is this (I think this idea would be valuable for many):
I would like to have members of my affiliate program be able to promote a more SE friendly URL like:
http://example.net/4/admin
instead of http://example.net/x.cgi?id=admin
Is there a way to setup a rewrite in the "4" directory to automatically insert the "x.cgi?id=" before the affiliate code?
I have been searching all over the net all day and finally found your site. Can you help?
Sincerely,
Mark Kessler
[edited by: jdMorgan at 4:31 am (utc) on Mar. 17, 2005]
[edit reason] Obscured specifics. Please see TOS. [/edit]
Welcome to WebmasterWorld!
See the phrase "back-reference" in the RewriteCond and RewriteRule sections of the Apache mod_rewrite documentation. Also see the Apache URL Rewriting Guide for examples. Links to both of these resources are available in our forum charter [webmasterworld.com]. This site uses the same technique to make the forum threads appear with static URLs.
Jim
Here's his response:
The shortening of the url is done by using .htaccess redirection normally reserved for 404 error management.
To setup such a redirection. Create an .htaccess file and enter the following lines in it. You will notice that instead of using the ErrorDocument command, I've made use of mod_rewrite That's because IE browser has a problem making such redirection if you use the normal ErrorDocument 404 code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^(.+) redir.cgi
Next, create a redir.cgi file and enter the following lines in it:
#!/usr/bin/perl
$affiliateid = $ENV{'REQUEST_URI'};
$affid = $affiliateid;
$affid =~ s/\/(.*?)\///g;
print "Location: [example.net...]
exit;
Next, ftp into your hosting account and upload the .htaccess file and redir.cgi in the same folder in ASCII mode. You can upload the files in your public_html folder directly and your member links will then be [example.net...] (where X will be the member's id). Or you can create a sub-folder (go for example). Then your links will be [profitblaze.net...] (where X will be the member's id).
Chmod redir.cgi to 755
Thanks again for your help!
RewriteRule ^([0-9]{1,3})/admin /x.cgi?id=$1 [L]
There's no need to create or call any external scripts or to do an external redirect which "exposes" the unfriendly URL, given your initial requirements.
The "{1,3}" allows for one- to three-digit id numbers -- you can change this to suit your needs.
Jim