簡體   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