homepage Welcome to WebmasterWorld Guest from 54.204.90.135
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

    
Replace spaced URL to Hyphens
How I can replace spaces in my url to hyphens using the mod_rewrite
sweetguyzzz




msg:4075474
 5:09 pm on Feb 6, 2010 (gmt 0)

Hello,

I am working to get my page like www.example.com/folder name/page name.html to open as www.example.com/folder-name/page-name.html means user opens a url included with hyphens but it is getting data from the spaces included url. For this I made this code but it works oppositely, so jdMorgan can you please help me

MY CODE:
Options +FollowSymLinks
RewriteEngine On

#Giving all php pages html extension
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=rspace:yes,N]

#For redirect search results to a page which is without any extension
RewriteCond %{ENV:rspace} ^yes$
RewriteRule ^(.*)$ /project1/$1? [R=301,L]

 

jdMorgan




msg:4075935
 9:12 pm on Feb 7, 2010 (gmt 0)

If you want to reverse the operation of this rule, then presuming that this code is located in /project/.htaccess, and that you wish to discard any appended query strings (as is done by your original code), it would be:

# Replace a hyphen with space, set redirect-required flag, and then re-start
# mod_rewrite processing in case there's another hyphen in this requested path
RewriteRule ^([^-]*)-(.*)$ $1\%20$2 [E=rhyphen:yes,NE,N]
#
# If redirect-required flag set by previous rule, do the redirect
RewriteCond %{ENV:rhyphen} ^yes$
RewriteRule ^(.*)$ /project1/$1? [R=301,NE,L]

The [NE] flag is used here to prevent the spaces from being multiply-encoded, e.g. as %2520 or %252520

If you are trying to change the URL of your page(s), then you will need to edit the links to that page; URLs are defined by links, and mod_rewrite cannot "change" a URL, it can only rewrite it to a differently-named file, or redirect it to a different URL.

Jim

neostar




msg:4080278
 4:27 am on Feb 15, 2010 (gmt 0)

gread help djMorgan, i was also bit lokking for that but how do i know how many spaces are there (to be replaced with hypens)?

jdMorgan




msg:4080476
 2:02 pm on Feb 15, 2010 (gmt 0)

You don't know. The "N" flag on the first rule re-starts mod_rewrite processing if that rule is invoked, so your mod_rewrite code will "loop" until all hyphens are changed. Then control will pass to the second rule to do a single external redirect to inform the client to request the corrected URL.

If you *do* know the maximum number of hyphens you might have in a URL, a simpler and more efficient solution might be to use a 'stack' of rewriterules: For example, the first changing four hyphens to spaces, the second changing three hyphens to spaces, the third changing two hyphens to spaces, and the last changing only one hyphen, for a total of ten. Note that each rule in sequence must rewrite either the same number of hyphens or a smaller number than the one that precedes it; Otherwise this method will not work properly.

It is also possible that these multiple sequential rewrites may trigger a well-know bug in mod_rewrite. If this happens, you notice that the output path is incorrect, and will appear to contain multiple 'extra' copies of parts of the path. If this happens, post back here. There is a work-around for this problem, but it is rather 'ugly' and inefficient.

Jim

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