dirk
dirk

Reputation: 2296

Htaccess Mod Rewrite: allow integers or a specific word

I am using .htaccess to tunnel traffic to my index.php. Below is one of the lines I am using:

RewriteRule ^page/([0-9]*)$ /index.php?&page=$1

What I want is to allow only numeric values, and the word 'all'. So the regexp should match:

How can matching against both values be acchieved? A simple solution would be ([0-9al]*), but that isn't perfect as it also allows for values like 'al9', 'lalalala' etc. Thanks!

Upvotes: 1

Views: 232

Answers (2)

Reeno
Reeno

Reputation: 5705

[0-9]* also matches zero numbers, you have to use + instead of *

To add 'all', use the pipe:

 RewriteRule ^page/([0-9]+|all)$ /index.php?&page=$1

disallow '0':

 RewriteRule ^page/([1-9]{1}[0-9]*|all)$ /index.php?&page=$1

[1-9]{1}[0-9]* means:

  • one number 1 to 9, followed by a uncertain occurrences (0, 1,...,unlimited) of the other numbers, even 0
  • so it matches a single number (besides 0) OR
  • 10, 11, 12, ..., 20, 21, ..., 23456

Upvotes: 3

anubhava
anubhava

Reputation: 784918

You can use:

RewriteRule ^page/((?!0+$)[0-9]+|all)/?$ /index.php?page=$1 [L,QSA,NC]

This will also avoid rewriting /page/0 /page/00 etc

Upvotes: 1

Related Questions