.htaccess - htaccess 301 重定向到 root 并删除查询字符串
问题描述
我正在尝试使用 htaccess 中的查询字符串重定向各种页面。
要重定向的 URL 的示例是:
/item.htm?item_id=ACPEMEG465P-VE&product
我想使用通配符来捕获 item_id* 之后的任何字母数字。
我想重定向到根域并删除查询字符串。因此,在查看了几个指南之后,我想出了:
RewriteCond %{QUERY_STRING} item_id(.*)$
RewriteRule ^item\.htm$ /? [L,R=301]
...但这似乎没有任何作用 - 我只是得到一个 404。谁能告诉我哪里出错了?我在 Apache 2.4.6
这是htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# security measures
Header always set X-Frame-Options "sameorigin"
Header always set Referrer-Policy "same-origin"
Header always set Feature-Policy "geolocation 'self'; camera 'none'; microphone 'none'"
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
RewriteCond %{QUERY_STRING} item_id(.*)$
RewriteRule ^item\.htm$ /? [L,R=301]
# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.4]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
#wpmu rewritecond companydomain.com
RewriteCond %{HTTP_HOST} ^companydomain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.companydomain\.com [OR]
#end wpmu rewritecond companydomain.com
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
我删除了压缩、mod_mime、mod_headers 和 mod_expires 部分,因为它们不相关
解决方案
您的新重定向规则出现在此规则之后:
RewriteRule . index.php [L]
该规则将所有非文件和非目录重写为,/index.php
并将REQUEST_URI
变量更新为/index.php
. 由于这个你的新规则:
RewriteCond %{QUERY_STRING} item_id(.*)$
RewriteRule ^item\.htm$ /? [L,R=301]
不会执行,因为不再REQUEST_URI
是/item.htm
。
一旦您将此规则移到顶部,它就会起作用,因为REQUEST_URI
它仍然是/item.htm
.
推荐阅读
- windows - PowerShell 脚本尝试复制不存在的文件夹
- c++ - 无法在 cpp 中按值返回向量
- postgresql - 对 10M 行表中 180k 行的 UPDATE 查询出乎意料地慢
- css - 如何将导入的 css 属性转换为线条样式?
- ruby - 使用 each_slice 并对 Ruby 进行排序
- visual-studio - msbuild 在 projects.assets.json 中找不到引用
- c++ - 如何将函数发送到 C++ 中的另一个进程?
- javascript - 在递归函数(reactjs)中更新状态的正确方法
- database - 无法从部署在 Azure 应用服务帐户上的 Web 应用连接到 Azure 数据库
- php - 带有验证的 Laravel 注册密钥