Htaccess重定向特定页面和查询字符串变量

Htaccess重定向特定页面和查询字符串变量

问题描述:

I'm trying to do a redirect with a .htaccess file matching these specific requirement:

  1. The page/script name is foo.php
  2. The querystring must contain p=1
  3. There could be any number of other querystring variables present

If these conditions are met, you should be redirected to: /new-url/

This is what I've come up with that doesn't work. It matches and redirects /foo.php but not /foo.php?p=1 or /foo.php?p=1&x=5

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} ^foo\.php$ [NC]
RewriteCond %{QUERY_STRING} (^|&)p=1(&|$) [NC]
RewriteRule . new-url/? [R=301,L]

Thanks very much in advance for any assistance!

我正在尝试使用符合这些特定要求的.htaccess文件进行重定向: p>

  1. 页面/脚本名称为 foo.php strong> li>
  2. 查询字符串必须包含 p = 1 strong>
  3. 可能存在任意数量的其他查询字符串变量 li> ol>

    如果满足这些条件,则应将您重定向到:/ new- url / p>

    这是我提出的不起作用的地方。 它匹配并重定向/foo.php但不是/foo.php?p=1或/foo.php?p=1&x=5

    nn
     RewriteEngine On 
    RewriteBase /  
     
    RewriteCond%{REQUEST_FILENAME} ^ foo \ .php $ [NC] 
    RewriteCond%{QUERY_STRING}(^ |&)p = 1(& | $)[NC] 
    RewriteRule。 新URL /?  [R = 301,L] 
      code>  pre> 
     
     

    非常感谢您的任何帮助! p> div>

Can you try:

RewriteCond %{QUERY_STRING} (^|&)p=1(&|$) [NC]
RewriteRule (^|/)foo\.php$ /new-url/? [R=301,L,NC]

Also test it in a different browser to avoid browser caching issues due to R=301