regexapache.htaccesshttp-redirectmod-rewrite

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.


Solution

  • 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.