9

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.

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

8 Comments

The DPI flag is new to me, so maybe I just don't know how this works, but doesn't this keep rewriting, removing one space at a time, until all of the spaces have been replaced? I supposed that works, but it seems a bit complicated. He also needs it to actually do a 301 redirect, and it doesn't look like this will do that. Correct me if I'm mistaken, though.
Thanks for adding the explanation! I learn something new on Stack Overflow every day. :)
what about urls that start with a space? like domain.com/ test test test /test
@PanagiotisGeorgeRaditsas: I believe it should work. If it doesn't please feel free to post a new question with link to this one and leave a comment here. I will sure try to fix that.
@anubhava Hello once again mate please take a look at this post it describes my issue as I stated above, thank you. stackoverflow.com/questions/18935257/…
|
5

Building on @anhubhava's answer, it's close, but that will also match %,2 or 0 in the URL, and it can cause a loop on apache 2.2 if you don't use the DPI parameter. The full script should look like this:

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 [N,E=NOSPACE:1,DPI]

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

I've also added the N (Next) parameter as this then forces the rules to be re-evaluated from the start straight after this rule if it matches. If this isn't there, you can get problems if you're using apache as a reverse proxy as it's unlikely that it'll get to the end of the rewrites before something else happens.

1 Comment

It seems it doesn't work with urls that contain numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.