Forum Moderators: phranque

Message Too Old, No Replies

.htaccess file to match an expression and redirect

         

jeyaganesh

1:14 pm on Aug 20, 2010 (gmt 0)

10+ Year Member




I am developing a url shortener system.

ex if the user clicks

www.example.com/projects/url/qw5r

then redirect to

www.example.com/projects/url/decoder.php?decode=qw5r


so i have written .htaccess file

RewriteEngine on
RewriteRule ^\w{4}$ decoder.php?decode=$1 [L]


but the script doesnt redirect to that page.
is my htaccess file is correct? please help me with correct code

wildbest

2:13 pm on Aug 20, 2010 (gmt 0)

10+ Year Member



You have to define 4-sequence alphanumeric (qw5r?!) group pattern [\w\d]{4} and put it in a variable ([\w\d]{4}). This htaccess file must be placed in your '/url' folder:

RewriteEngine on
#
RewriteRule ^/?([\w\d]{4})$ /decoder.php?decode=$1 [L]

I'm not sure if '\w' includes digits. This is why I combined it like this - [\w\d].

jeyaganesh

4:57 pm on Aug 20, 2010 (gmt 0)

10+ Year Member



I used your code

RewriteRule ^/?([\w\d]{4})$ /decoder.php?decode=$1 [L]


but I get 500 internal server error..

anyway I can solve this issue?



P.S : I am in the urgency to complete the project.

wildbest

5:54 pm on Aug 20, 2010 (gmt 0)

10+ Year Member



Try this:
RewriteRule ^/?([a-z0-9]{4})$ /decoder.php?decode=$1 [NC,L]

jdMorgan

6:09 pm on Aug 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First thing to do when you get a 500-Server error is to look at the server error log file... It will often tell you exactly what is wrong.

It could be that you're on Apache 1.3.x, in which case you cannot use PCRE, but must instead use POSIX regular expressions:

# Internally rewrite shortened URL requests to de-shortened URL lookup script filepath plus query string
RewriteRule ^([a-z0-9_]{4})$ decoder.php?decode=$1 [NC,L]

It could also be that you have no other mod_rewrite code in the file, and mod_rewrite is not yet set up and enabled:

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
# Internally rewrite shortened URL requests to de-shortened URL lookup script filepath plus query string
RewriteRule ^([\w\d]{4})$ decoder.php?decode=$1 [L]

Note the comments. Most experts use accurate and concise comments. Beginners should take a lesson from that... :)

Note also that the [NC] flag is needed in the first version to make the character-compare case-insensitive in an efficient manner. It is not needed with the PCRE version.

Jim

jeyaganesh

8:51 am on Aug 21, 2010 (gmt 0)

10+ Year Member



Thanks jd morgan, This code works now. also thanks for ur advice in adding comments while coding.


Thanks wildbest for your help too..



Thanks
Jeyaganesh