Welcome to WebmasterWorld Guest from 54.147.44.93

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

     

sweetguyzzz

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

5+ Year Member



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

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

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



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

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

5+ Year Member



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

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

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



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month