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?
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.
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:
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.
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.
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]
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:
0 <= ID <= 999
: \d{1,3}
1000 <= ID <= 6999
: [1-6]\d{3}
7000 <= ID <= 7599
: 7[0-5]\d{2}
7600 <= ID <= 7603
: 760[0-3]
You can test it Here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With