HTACCESS redirect using URL parameter ID number range

旧巷老猫 提交于 2021-01-02 06:34:41

问题


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?


回答1:


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]



回答2:


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



来源:https://stackoverflow.com/questions/24161897/htaccess-redirect-using-url-parameter-id-number-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!