繁体   English   中英

htaccess 301 重定向到 root 并删除查询字符串

[英]htaccess 301 redirect to root and remove query string

我正在尝试使用 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM