Daniel Lee
Daniel Lee

Reputation: 367

HTACCESS redirect using URL parameter ID number range

I'm hoping someone can help as this is proving difficult to figure out.

I am trying to redirect via HTACCESS and mod_rewrite a number of pages that have a URL parameter ID value within a particular range (from 1 to 7603).

Here is what I have so far:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} &?id=\b([1-9][0-9]{0,2}|[1-6][0-9]{3}|7[0-5][0-9]{2}|760[0-3])\b [NC]
RewriteRule ^example\.php$ http://www.website.com/? [R=301,L]
</IfModule>

It currently does redirect the page if there is an ID URL parameter, but it redirects any ID number, not just those within the specified range, e.g. it will redirect ID=10000 even though is shouldn't.

Does anyone know what I have done wrong and how I can fix it?

Upvotes: 6

Views: 1233

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133428

With your shown samples, could you please try following. Please make sure you clear your cache before testing your URLs. Conditions for query string will be checked to make sure values after id= are well under 7603 here, rest redirection part is as per OP's shown samples only.

RewriteEngine ON
RewriteCond %{QUERY_STRING} id=[0-6]?[0-9]?[0-9]?[0-9]?(?![0-9]+) [NC,OR]
RewriteCond %{QUERY_STRING} id=7?[0-5]?[0-9]?[0-9]?(?![0-9]+) [NC,OR]
RewriteCond %{QUERY_STRING} id=760[0-3]?(?![0-9]+) [NC]
RewriteRule ^example\.php/?$ http://www.website.com/? [R=301,L]

Upvotes: 3

Ali Nikneshan
Ali Nikneshan

Reputation: 3502

You can use this regex: \b((\d{1,3})|([1-6]\d{3})|(7[0-5]\d{2})|(760[0-3]))\b

It should be split to this steps:

  1. 0 <= ID <= 999: \d{1,3}
  2. 1000 <= ID <= 6999: [1-6]\d{3}
  3. 7000 <= ID <= 7599: 7[0-5]\d{2}
  4. 7600 <= ID <= 7603: 760[0-3]

You can test it Here

Upvotes: 2

Related Questions