Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 重写页面&;将所有查询参数转换为.htacess中的单个URL_Regex_Apache_.htaccess - Fatal编程技术网

Regex 重写页面&;将所有查询参数转换为.htacess中的单个URL

Regex 重写页面&;将所有查询参数转换为.htacess中的单个URL,regex,apache,.htaccess,Regex,Apache,.htaccess,我已经读了很多关于这方面的帖子,但仍然无法理解 我有一个使用了很多查询参数的旧页面。我想将此页面的流量重定向到我的主页。当存在参数时,普通301重定向不起作用 我想: /page.php /page.php?a=1 /page.php?a=2 全部重定向到example.com/ 此代码已在我的htaccess中: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ -

我已经读了很多关于这方面的帖子,但仍然无法理解

我有一个使用了很多查询参数的旧页面。我想将此页面的流量重定向到我的主页。当存在参数时,普通301重定向不起作用

我想:

  • /page.php
  • /page.php?a=1
  • /page.php?a=2
全部重定向到example.com/

此代码已在我的htaccess中:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
发生的事情是,只有最后一行正在触发。如果我导航到example.com/page.php,它会按预期重定向

但如果我导航到example.com/page.php?a=1,它就会重定向到example.com/?a=1,并给出404错误


有人有解决办法吗?谢谢

无需捕获
查询字符串
,因为您希望将所有流量重定向到
page.php
包括
page.php?查询
,因此,只需匹配此页面URI即可:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteRule ^(.*)$ https://www.example.com/? [R=301,L]

注意:清除浏览器缓存,然后进行测试

谢谢,但它不起作用。我刚拿到404。这可能与上面的代码冲突吗?例如,语句中是否可以有多个重写引擎?@Sean将这些规则放在最前面top@Sean重写引擎是每个文件一次将其置于顶部使其工作!谢谢你能解释一下为什么会有不同吗?@Sean简单地说,你处理第一个规则中所有请求的结构与第二个规则之前的页面URI相匹配
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteRule ^(.*)$ https://www.example.com/? [R=301,L]