当用户输入旧网址时,使用htaccess从301重定向中删除问号 [英] remove question mark from 301 redirect using htaccess when the user enters the old URL

查看:61
本文介绍了当用户输入旧网址时,使用htaccess从301重定向中删除问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地将www.example.com/importlist之类的网址重定向到www.example.com/index.php?importlist.

I successfully redirected urls like www.example.com/importlist to www.example.com/index.php?importlist.

但是,当用户将网址www.example.com/index.php?importlist输入到www.example.com/importlist(以在地址栏中显示)时,我需要执行重定向301.

However i need to do a redirect 301 when the user enters the url www.example.com/index.php?importlist to www.example.com/importlist (to show it in the address bar).

我尝试了以下代码:

RewriteEngine On

RewriteRule ^index\.php(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(?!index\.php|web/.*)(.+)$ index.php?$1 [L]

它工作得很好,但是问号仍保留在URL中.这意味着:当用户输入www.example.com/index.php?importlist之类的网址时,它将重定向到www.example.com/?importlist.

It works pretty fine, however the question mark remains in the url. which means: when the user enters a url like www.example.com/index.php?importlist it redirects to www.example.com/?importlist.

我尝试使用重写条件(%{AAAA})删除此问号,并将其添加到301重定向中,添加?最后等,但似乎没有任何效果,在最坏的情况下,它最终会在重定向循环中结束.我已经在整整2天的时间内尝试了类似问题中发布的所有其他答案,但似乎没有任何效果,所以我不得不做自己的问题.

Ive tried to remove this question mark using rewrite conditions (%{AAAA}) and adding them to the 301 redirect, adding ? at the end, etc, but nothing seems to work and in the worst case it ends up in a redirect loop. Ive also tried all other answers posted in similar question during 2 whole days, and nothing seems to work, so i had to do my own question.

附加说明:它必须适用于所有页面,不仅限于导入列表,例如:如果用户输入www.example.com/index.php?perso,则必须将301重定向到www.example.com/perso,依此类推其他所有页面.

Adittional note: it must work with all pages, not only importlist, example: if the user enters www.example.com/index.php?perso it must redirect 301 to www.example.com/perso, and so with all other pages.

推荐答案

您可以使用以下规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

RewriteCond %{THE_REQUEST} \s/+\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

# internal forward from pretty URL to actual one
RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]

这篇关于当用户输入旧网址时,使用htaccess从301重定向中删除问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆