Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 Redirect to replace all spaces to hyphens

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

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? :)

like image 685
Macgyver Avatar asked Apr 28 '11 15:04

Macgyver


People also ask

How do I fix a 301 redirect error?

There are two ways to fix these errors; Replace the redirect chain with a single 301 redirect. Instead of Page 1 > Page 2 > Page 3 > Page 4, the redirect becomes Page 1 > Page 4. Replace internal links to redirected pages with direct links to the final URL. This prevents Google and other bots from crawling the redirect chains.

Can I 301 redirect both pages to a different URL?

We reused the /skyscraper-technique/ URL. If neither of the old URLs is a good match for your new post/page, then it’s also perfectly fine to 301 redirect both pages to a totally new URL. For example, if we were to merge those two Hubspot posts into this guide:

Are 301 redirects good for SEO?

It means that 301 redirects are perfectly fine as far as PageRank is concerned; if you have an inbound link that goes to a nonexistent page of your site, but that page has a 301 redirect that leads to the new page, the new page will receive 100 percent of that PageRank. Because of this, 301 redirects are a powerful SEO tool.

How do 301 redirects work with backlinks?

You know, the one with this backlink profile: Because Brian moved the post from the old domain to the new with a 301 redirect, all of those links now effectively point to that same page on backlinko.com instead. The page has effectively just moved to a new home.


1 Answers

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]

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

So a URI of /tag/bob%20hope%20is%20funny will become /tag/bob-hope-is-funny with 301

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.

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

like image 176
anubhava Avatar answered Oct 12 '22 08:10

anubhava