Welcome to WebmasterWorld Guest from 54.196.214.35

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Replacing "%20" with "-" in URLs using mod_rewrite?

     
10:10 pm on Jul 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 18, 2003
posts:58
votes: 0


Hi all,

Was wondering if anyone knew of a solution to replacing %20 in URLS with - (hyphens) using mod_rewrite?

Seriously need help on this and ANY help is appreciated.

Thanks!

Aaron

5:02 pm on Aug 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


martekbiz,

Here's a brute-force (but "safe") solution that handles %20 occurring within the requested filepath, but not at the beginning or the end. This will handle up to three spaces (%20). If you need more, just add more rules, following the established template. If you don't need to handle three instances, delete the longer lines at the beginning, and leave the shorter ones.

By the time this .htaccess code is processed, the server has translated all %-encoded characters to their ASCII character equivalents. Since %20 is a space, this code simply looks for spaces and converts them to hyphens.


# Rewrite up to three instances of "%20" to "-" in URL and do a 301-Moved Permanently redirect.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ([^\ ]+)\ ([^\ ]+)\ ([^\ ]+)\ (.+) http://www.yourdomain.com/$1-$2-$3-$4 [R=301,L]
RewriteRule ([^\ ]+)\ ([^\ ]+)\ (.+) http://www.yourdomain.com/$1-$2-$3 [R=301,L]
RewriteRule ([^\ ]+)\ (.+) http://www.yourdomain.com/$1-$2 [R=301,L]

HTH,
Jim
11:00 pm on Aug 2, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 18, 2003
posts:58
votes: 0


Hey Jim,

Thanks a lot for the code help.

I am a bit confused.

I was able to throw togehter the following:

RewriteRule threads/(.*)[[:space:]](.*)$ spaceredirectthreads/$1_$2 [L]

# If the URI contains ``spaceredirect''
# it means that I will have to make an external redirection
RewriteCond %{REQUEST_URI} ^/KCS/spaceredirectthreads/(.+)$ [NC]
RewriteRule . /KCS/threads/%1 [R,L]

RewriteRule ^threads(/([^/]+))?/?$ threads.php?type=$2 [L]
RewriteRule ^threads/([^/]+)/([^/]+)/?$ threads.phptype=$1&groupName=$2

This is working for things like: KCS/threads/Man_On_the_moon

It is NOT working for something like: KCS/threads/Man_on_the_moon/Hello_there

Instead it returns:

KCS/threads/Man%20on%20the%20moon/Hello%20there

Any clue how I go about getting to work for the second example?

I'm new to this mod_rewrite stuff... it's all so confusing!

Thanks!

Aaron