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

Apache Web Server Forum

    
replace space with hyphen
replace space with hyphen
fahad direct




msg:4249328
 11:28 am on Jan 5, 2011 (gmt 0)

I have already read the useful thread on:
[webmasterworld.com...]

My redirect works fine but before redirect i want to replace spaces with hyphen '-' in $8 below:

RewriteRule ^([0-9]*)1([0-9]+)0([0-9]+)-Page([0-9]*)-([0-9]*)-Var(.*)/P([~A-Za-z0-9]*)-Sort(.*)/(.*)$ /$8.htm [R=301,L]

Any help will be highly apprecieated.

 

fahad direct




msg:4249403
 4:26 pm on Jan 5, 2011 (gmt 0)

I could do it by double redirect like:

RewriteRule ^([0-9]*)1([0-9]+)0([0-9]+)-PG([0-9]*)-([0-9]*)-V(.*)/P([~A-Za-z0-9]*)-S(.*)/(.*)$ /$8.htm [R=301,L]


RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=rspace:yes,N]
RewriteCond %{ENV:rspace} yes
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]

Is there any way to merge both in one?

g1smd




msg:4249504
 7:52 pm on Jan 5, 2011 (gmt 0)

One comment about the above code.

The multiple (.*) patterns will slow your server to a crawl. The first (.*) pattern will match ALL of the remainder of the URL request and the pattern-matching will have to back off and retry hundreds of times to find the correct match. It will then have to do the same again when the next (.*) pattern is encountered, then again for the next one. The parser might have to try tens of thousands of possible matches before it finds the right one.

Change all of the (.*) patterns to instead be those that can be read left-to-right in a single pass. Something like ([^/]+)/ might suffice in at least one case. Others might be ([a-z0-9]+)/ or similar.

The will massively improve server efficiency.

jdMorgan




msg:4249577
 10:22 pm on Jan 5, 2011 (gmt 0)

Yes they can be combined, but it requires a complicated technique to avoid a known Apache bug that will occur if you try to rewrite Apache's internal request_rec more than once:

# Skip the next four rules if the originally-requested URL contains no spaces
RewriteRule !^[^\ ]*\ .*$ - [S=4]
#
# SPACE-TO-HYPHEN-CONVERSION REDIRECT RULESET
#
# For "special-format" URL-paths, drop all but the "S" URL-path-part, append ".htm", put
# the result into the myURLpath variable unless already done, and skip the next rule
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^[0-9]+-PG[0-9]*-[0-9]*-V[^/]*/P[~A-Za-z0-9]*-S([^/]*)/ - [E=myURLpath:$1.htm,S=1]
#
# Else copy the requested URL-path as-is to the myURLpath variable unless already done
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^(.*)$ - [E=myURLpath:$1]
#
# Replace one space in the copied URL-path with a hyphen, then re-start mod_rewrite processing
RewriteCond %{ENV:myURLpath} ^([^\ ]*)\ (.*)$
RewriteRule ^ - [E=myURLpath:%1-%2,N]
#
# If no spaces remain in the copied URL-path, externally redirect to the new hyphenated URL
RewriteRule ^ http://%{HTTP_HOST}/%{ENV:myURLpath} [R=301,L]
#
# END SPACE-TO-HYPHEN-CONVERSION REDIRECT RULESET
#
# Redirect "special-format" URL-path requests that did not contain spaces in the "S" path-part
RewriteRule ^[0-9]+-PG[0-9]*-[0-9]*-V[^/]*/P[~A-Za-z0-9]*-S([^/]*)/ http://%{HTTP_HOST}/$1.htm [R=301,L]

Read the comments carefully.

You may not need the last rule, if the "special format" URLs always contain spaces in the "S" part.

Hopefully, no typos... :)

Jim

[edit] Corrected typos as noted below. [/edit]

[edited by: jdMorgan at 5:09 pm (utc) on Jan 9, 2011]

fahad direct




msg:4249912
 5:15 pm on Jan 6, 2011 (gmt 0)

Thank you Jim and g1smd really great help :)

fahad direct




msg:4250261
 1:46 pm on Jan 7, 2011 (gmt 0)

I have tried the code but it is not working properly and in the result i am getting only - after domain name like:

[mydoamin-name.com...]

Here is what i am trying for my url:

RewriteRule !^[^\ ]*\ .*$ - [S=4]
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^([7]+)1([0-9]+)0([0-9]+)-PG([0-9]*)-([0-9]*)-V(.*)/P([~A-Za-z0-9]*)-S([^/]*)/ [E=myURLpath=$1,S=1]
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^(.*)$ - [E=myURLpath:$1]
RewriteCond %{ENV:myURLpath} ^([^\ ]*)\ (.*)$
RewriteRule ^ - [E=myURLpath:$1-$2,N]
RewriteRule ^ [%{HTTP_HOST}...] [R=301,L]


Jim, could you correct me please if i am doing anything wrong? :)

jdMorgan




msg:4250313
 3:31 pm on Jan 7, 2011 (gmt 0)

Sorry, there *was* a typo in the flags of the first RewriteRule in the code I posted above. I have corrected it as noted.

Also, please note that only one set of parentheses appears in my second rule, while you have many more in yours. This is important, and will prevent your code from working.

Jim

fahad direct




msg:4250449
 8:14 pm on Jan 7, 2011 (gmt 0)

Still getting same result:

RewriteRule !^[^\ ]*\ .*$ - [S=4]
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^[7]+1[0-9]+0[0-9]+-PG[0-9]*-[0-9]*-V[^/]*/P[~A-Za-z0-9]*-S([^/]*)/
[E=myURLpath:$1,S=1]
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^(.*)$ - [E=myURLpath:$1]
RewriteCond %{ENV:myURLpath} ^([^\ ]*)\ (.*)$
RewriteRule ^ - [E=myURLpath:$1-$2,N]
RewriteRule ^ [%{HTTP_HOST}...] [R=301,L]

jdMorgan




msg:4250963
 2:42 pm on Jan 9, 2011 (gmt 0)

Sorry, there was another typo -- this time in the second RewriteRule. See correction above.

A good reminder that mod_rewrite code must be completely, exactly correct... :o

Jim

fahad direct




msg:4250988
 4:26 pm on Jan 9, 2011 (gmt 0)

I might be doing something wrong as it is being redirected only to : [%{HTTP_HOST}...]

RewriteRule !^[^\ ]*\ .*$ - [S=4]
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^[7]+1[0-9]+0[0-9]+-PG[0-9]*-[0-9]*-V[^/]*/P[~A-Za-z0-9]*-S([^/]*)/ - [E=myURLpath:$1,S=1]
RewriteCond %{ENV:myURLpath} =""
RewriteRule ^(.*)$ - [E=myURLpath:$1]
RewriteCond %{ENV:myURLpath} ^([^\ ]*)\ (.*)$
RewriteRule ^ - [E=myURLpath:$1-$2,N]
RewriteRule ^ [%{HTTP_HOST}...] [R=301,L]

g1smd




msg:4250998
 4:58 pm on Jan 9, 2011 (gmt 0)

The fragment

-PG[0-9]*-[0-9]*-V

allows

-PG--V to be a part of a valid URL request (i.e. NO digits).

I am guessing that some * should be + to enforce that at least one digit must be present.

fahad direct




msg:4251004
 5:07 pm on Jan 9, 2011 (gmt 0)

Here is my one url example:

[sub-domain-name.com...] to be replaced phrase

jdMorgan




msg:4251007
 5:11 pm on Jan 9, 2011 (gmt 0)

Sometimes it would be better if I just waited to post... I added the note on my original code that said "Hopefully, no typos" because I was tired... and this shows in the code.

Yet another pair of typos in the third rule, now corrected.

Jim

fahad direct




msg:4251017
 5:34 pm on Jan 9, 2011 (gmt 0)

I really appreciate your efforts. I have tried in this way and still got the same results.

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