views:

72

answers:

2

Hi,

I'm using Apache/PHP to support shorlinks to documents and I'm having trouble with the Regex to redirect correctly.

My links take the form of 8 letters/numbers, something like '1abc45fd', I would like to have them redirect to /shortlink.php?link=1abc45fd but it's just not working correctly. I'm using the following expression: "RewriteRule ^([a-zA-Z0-9]+)$" in my .htaccess file but that redirects all URLs, not just ones that are only 8 chars. How can I modify the rule to limit to exactly matching the 8 chars?

Thanks in advance.

+2  A: 
RewriteRule ^([a-zA-Z0-9]{8})$ shortlink.php?link=$1

You may want to familiarise yourself with Regular Expressions syntax some more, I found this Regex Reference page to be a good start.

Aistina
Thanks, I'll give that a go! I've been trying to learn Regex as and when I needed to use it but it still doesn't make any sense to me, I guess I'll have to keep trying!
Jason
"{8}" means exactly eight characters from the preceding set, and "$1" references the first parenthetical match (sub-expression) in the expression
Sonny
A: 
RewriteRule ^([a-zA-Z0-9]{8})$

EDIT: Aistina beat me to it

Sonny