homepage Welcome to WebmasterWorld Guest from 54.242.241.20
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

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

10+ Year Member



 
Msg#: 317 posted 10:10 pm on Jul 31, 2003 (gmt 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

 

jdMorgan

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



 
Msg#: 317 posted 5:02 pm on Aug 2, 2003 (gmt 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

martekbiz

10+ Year Member



 
Msg#: 317 posted 11:00 pm on Aug 2, 2003 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved