301重定向,以取代所有空格连字符。 [英] 301 Redirect to replace all spaces to hyphens

查看:324
本文介绍了301重定向,以取代所有空格连字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这里是我的问题。我接手一个网站,有有一堆的有20%,在谷歌索引的索引的网页。这完全是因为这个人决定只使用标记名称作为标题和URL塞。因此,该网址是这样的:

So here's my problem. I took over a site that has has a bunch of pages indexed that have %20 indexed in Google. This is simply because the person decided to just use the tag name as the title and url slug. So, the urls were something like this:

http://www.test.com/tag/bob%20hope
http://www.test.com/tag/bob%20hope%20is%20funny

我添加了一个新的领域,为URL蛞蝓和字符串替换成短线的所有空间。虽然我没有问题链接到这些新页面,并获取数据,我需要301重定向旧的URL到新的URL,这将是这样的:

I have added a new field for the url slug and string replaced all spaces with dashes. While I have no problem linking to these new pages and getting the data, I need to 301 redirect the old URLs to the new URLs, which would be something like:

http://www.test.com/tag/bob-hope
http://www.test.com/tag/bob-hope-is-funny

因此​​,需要能够以考虑多个空格。任何问题? :)

So, it needs to be able to account for multiple spaces. Any questions? :)

推荐答案

在你的.htaccess文件中使用这些规则:

Use these rules in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]

# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]

这将替换所有空格字符( \ S %20 )以连字符 -

This will replace all space characters (\s or %20) to hyphen -

所以 /标签/鲍勃%20hope%20是%20funny 将成为 /标签/鲍勃希望-是搞笑的URI 301

简要说明:如果有多于1空间URI那么第一个重写规则被激发递归替换用连字符每个空格字符 - ,直到有没有剩余空间。此规则将只在内部改写。

Brief Explanation: If there are more than 1 space in URI then 1st RewriteRule is fired recursively replacing each space character with hyphen - until there is no space left. This rule will only rewrite internally.

在没有剩余空间可第二次重写规则被触发它只是使用了 301重定向来转换的URI。

Once no space is left 2nd RewriteRule is fired which just uses a 301 redirect to the converted URI.

这篇关于301重定向,以取代所有空格连字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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