簡體   English   中英

通過 htaccess 從 301 重定向中刪除查詢字符串

[英]Remove Query String from 301 Redirect through htaccess

我面臨幾個 301 重定向問題。

問題 1:

我想將一些鏈接重定向到同一站點的另一個 URL:

Redirect 301 /path-example.html /dir/subdir/

重定向正在工作,但它正在生成這樣的查詢字符串:

example.com/dir/subdir/?str=path-example

如何刪除查詢?str=path-example

問題 2:

我想將一些鏈接重定向到主頁:

Redirect 301 /some-path.html /

但它留下了? 在這樣的斜杠之后:

example.com/?

如何刪除/? 從轉發的網址?

現有規則:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^$ / [R=301,L]

謝謝!

請將這些規則放在 htaccess 文件的頂部(以防其中有更多規則)。 在測試您的 URL 之前,請確保清除瀏覽器緩存。

<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteRule ^index\.php/?$ - [L,NC]

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]

##Redirection to site's home page here for html urls.    
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]

RewriteCond %{QUERY_STRING} ^s [NC]
RewriteRule ^/?$ / [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>


編輯:我沒有 OP 的其他規則集的第一個答案如下:

RewriteEngine ON
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.    
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM