繁体   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