Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Replace spaced URL to Hyphens

How I can replace spaces in my url to hyphens using the mod_rewrite

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

Junior Member

5+ Year Member

joined:May 30, 2009
posts: 71
votes: 0


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

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]
9:12 pm on Feb 7, 2010 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
votes: 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.

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

New User

5+ Year Member

joined:Feb 14, 2010
posts: 34
votes: 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)?
2:02 pm on Feb 15, 2010 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
votes: 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.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members