简体   繁体   English

通过 htaccess 从 301 重定向中删除查询字符串

[英]Remove Query String from 301 Redirect through htaccess

I am facing a couple of 301 redirect issues.我面临几个 301 重定向问题。

Issue 1:问题 1:

I want to redirect some links to another URL of the same site:我想将一些链接重定向到同一站点的另一个 URL:

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

The redirect is working but it's generating query string like this:重定向正在工作,但它正在生成这样的查询字符串:

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

How do I remove the query sting ?str=path-example ?如何删除查询?str=path-example

Issue 2:问题 2:

I want to redirect some links to the homepage:我想将一些链接重定向到主页:

Redirect 301 /some-path.html /

But it's leaving a ?但它留下了? after a trailing slash like this:在这样的斜杠之后:

example.com/?

How do I remove /?如何删除/? from the forwarded URL?从转发的网址?

Existing rules:现有规则:

<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]

Thanks!谢谢!

Please place these rules top of your htaccess files(in case you have more rules in it).请将这些规则放在 htaccess 文件的顶部(以防其中有更多规则)。 Please make sure to clear your browser cache before testing your URLs.在测试您的 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>


EDIT: My first answer without OP's other rules set is as follows:编辑:我没有 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