Welcome to WebmasterWorld Guest from 54.167.0.111

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

replace space with hyphen

replace space with hyphen

     

fahad direct

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

5+ Year Member



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

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

5+ Year Member



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

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

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



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

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

5+ Year Member



Thank you Jim and g1smd really great help :)

fahad direct

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

5+ Year Member



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

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

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



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

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

5+ Year Member



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

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

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



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

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

5+ Year Member



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

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

5+ Year Member



Here is my one url example:

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

jdMorgan

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

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



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

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

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month