简体   繁体   English

使用301重定向htaccess从URL中删除某些字符?

[英]Remove Some Characters From URL Using 301 Redirect htaccess?

I want to remove some characters ?m=1 & ?m=0 from the end of my URLs. 我想从网址末尾删除一些字符?m=1?m=0 For this purpose I used a JavaScript from a blog post How To Forcefully Redirect Blogger Mobile Template To Desktop Template? 为此,我使用博客文章中的JavaScript 如何将Blogger移动模板强制重定向到桌面模板? and edit it with my desired till below one and its working fine. 并以我想要的方式编辑它,直到低于1且工作正常。

<script type='text/javascript'>
var curl = window.location.href;if (curl.indexOf('m=1') != -1) {curl = curl.replace('m=1', '');window.location.href = curl;}
var curl = window.location.href;if (curl.indexOf('m=0') != -1) {curl = curl.replace('m=0', '');window.location.href = curl;}
</script>

Its removing my desired extra characters and then redirecting to the new URL but according to Google its not SE friendly and I have to use 301 Redirect via .htaccess that will tell every search engine too that this URL is now change to this. 它删除了我想要的多余字符,然后重定向到新的URL,但是根据Google的说法,它对SE不友好,我必须通过.htaccess使用301 Redirect ,它还会告诉每个搜索引擎该URL现在也已更改为此。

My URLs are showing as http://www.blog.com/2014/08/post-title.html?m=1 & http://www.blog.com/2014/08/post-title.html?m=0 now... So can I do the same using .htaccess and 301 Redirect ? 我的网址显示为http://www.blog.com/2014/08/post-title.html?m=1http://www.blog.com/2014/08/post-title.html?m=0现在http://www.blog.com/2014/08/post-title.html?m=0 ...所以我可以使用.htaccess301 Redirect来做同样的事情吗?

If you only have this query string on your entire website, you can try this : 如果您在整个网站上只有此查询字符串,则可以尝试以下操作:

RewriteEngine    On
RewriteCond      %{QUERY_STRING}    ^m=1$  [OR]
RewriteCond      %{QUERY_STRING}    ^m=0$
RewriteRule      (.*)               $1?     [R=301]

You can do this by using mod_rewrite. 您可以使用mod_rewrite来做到这一点。 You can check the query string in a condition with %{QUERY_STRING} and use the [R=301] flag to make it a permanent redirect. 您可以使用%{QUERY_STRING}检查条件中的查询字符串,并使用[R=301]标志使其成为永久重定向。 The QSD flag will discard the query string. QSD标志将丢弃查询字符串。

In Apache 2.4 and above you can use the following: 在Apache 2.4及更高版本中,可以使用以下命令:

RewriteCond %{QUERY_STRING} m=[01]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [QSD,R,L]

In lower versions of Apache the QSD flag does not exist. 在较低版本的Apache中,QSD标志不存在。 You'll have to use a trick where you overwrite the query string by appending an empty query string to the rewrite target. 您将必须使用技巧,通过将空查询字符串附加到重写目标来覆盖查询字符串。

RewriteCond %{QUERY_STRING} m=[01]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}? [R,L]

Change the R to R=301 when the rule works as expected. 更改RR=301时,规则按预期工作。 Please note that you should place this before any other rule you have for your blog. 请注意,您应该将其放置博客的其他任何规则之前 There is most likely a catch-all rule in the end that sends the request to your blog backend, and you have to redirect before that happens. 最后,最有可能将请求发送到您的博客后端的“包罗万象”规则,您必须先重定向。

Here I got another solution for this so if upper isn't working for you then try this by adding below codes in top of .htaccess file... 在这里,我得到了另一种解决方案,因此,如果upper无法满足您的要求,请尝试在.htaccess文件顶部添加以下代码...

RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteCond %{QUERY_STRING} ^m=0$
RewriteRule ^(.*)$ /$0? [R=301,L]

The above code will only work when your URL will be like http://www.example.com?m=1 but not when http://www.example.com?m=1&foo=bar . 上面的代码仅在您的网址为http://www.example.com?m=1 ,而在http://www.example.com?m=1&foo=bar无效。

you can use simple java script to redirect users this script should be placed above top of all the css and js because it has to be loaded as soon as the user visit 您可以使用简单的Java脚本重定向用户,此脚本应放置在所有CSS和JS的上方,因为一旦用户访问它就必须立即加载

<script>
if (screen.width <= 800) {
     document.location = "http://mobileversionofyourwebsite .com/";
  }
</script>

source : auto liker sayoutlouder 来源: 自动点 赞者sayoutlouder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM