Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

RewriteRule ^([a-zA-Z0-9]{8})$

EDIT: Aistina beat me to it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top