Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess rewrite ONLY if first part of path is numeric

Is there an htaccess rule that will only rewrite if the first part of a path is numeric, so that http://www.example.com/123/whatever hits the rewrite rule, but http://www.example.com/user/whatever does not?

like image 471
bjork24 Avatar asked Dec 23 '10 18:12

bjork24


2 Answers

Here is a rewrite rule for my little site I am building

RewriteEngine on
RewriteRule ([a-zA-Z])/ index.php?k=$1
RewriteRule ([0-9]+)/ index.php?id=$1

So you can see that the regex rule [0-9]+ will match any numbers successively. The [a-zA-Z] will match letters.

like image 92
Thomas Havlik Avatar answered Nov 18 '22 04:11

Thomas Havlik


You can match numbers in your pattern. For example:

RewriteRule ^([0-9]+)/(.*) /foo/$2?bar=$1

Will rewrite http://www.example.com/123/whatever to http://www.example.com/foo/whatever?bar=123 but leave /user/whatever alone.

like image 31
Laurence Gonsalves Avatar answered Nov 18 '22 04:11

Laurence Gonsalves