301 Redirect for URL With Spaces

  • I have a situation where I want to redirect all the incoming URL with %20 in the URL to be replaced with - for all occurrences.

    Now I get this link -

    301 Redirect to replace all spaces to hyphens on this page

    which gives this solution

    1. Options +FollowSymlinks -MultiViews
    2. RewriteEngine on
    3. RewriteCond %{THE_REQUEST} (\s|%20)
    4. RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
    5. RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]

    which works perfect but there is one small issue with this solution. This also redirects the request for images/docs/pdf or other resources and results in 404

    Can i have something which will redirect only certain sections and not all incoming links like it will replace and redirect only for

    1. www.test.com/colleges/this%20is%20my%20link/123 to
    2. www.test.com/colleges/this-is-my-link/123
    3. or
    4. www.test.com/schools/this%20is%20my%20link/123 to
    5. www.test.com/schools/this-is-my-link/123

    and leave all other requests intact??

  • drewm

    Approved the thread.