bjork24
bjork24

Reputation: 3183

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?

Upvotes: 8

Views: 15498

Answers (2)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143074

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.

Upvotes: 5

Thomas Havlik
Thomas Havlik

Reputation: 1388

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.

Upvotes: 13

Related Questions