Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 658
Daniel Lee Avatar asked Jun 11 '14 11:06

Daniel Lee


People also ask

How do I redirect to another URL using a query string?

The code presented below is the 301 re-write for needed redirect using the query string. In this we’re simply rewriting the domain to “new_domain.com” while adding the value of the query string to the end of the URL.

How to include the query string in the refpage URL parameter?

If you want to include the query string from the original URL and make this part of the refpage URL parameter then you need to append the query string as an additional step. However, I think it is "easier" to grab the entire URL (URL-path + query string) from the THE_REQUEST Apache server variable and pass this instead:

How to redirect PAGE-1 to page-2 on the same domain?

The first path here (current domain page/URL) will include the slash at the beginning. You can not include the https protocol and domain host for the current page/URL (in the first path here). This htaccess code will redirect page-1.html to page-2.html on the same domain.

What does [r=301] and [L] mean in redirects?

The final part of the rule – [R=301,L] – indicates the type of redirect being used “R=301” and the flag “L” which basically means that this is the end of the rule. The rewrite will stop processing at this point.


2 Answers

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]
like image 186
RavinderSingh13 Avatar answered Sep 28 '22 05:09

RavinderSingh13


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

like image 20
Ali Nikneshan Avatar answered Sep 28 '22 07:09

Ali Nikneshan