Welcome to WebmasterWorld Guest from 54.146.217.179

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

replace space with hyphen

replace space with hyphen

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

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 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.
4:26 pm on Jan 5, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 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?
7:52 pm on Jan 5, 2011 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 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.
10:22 pm on Jan 5, 2011 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 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]

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

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 0


Thank you Jim and g1smd really great help :)
1:46 pm on Jan 7, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 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? :)
3:31 pm on Jan 7, 2011 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 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
8:14 pm on Jan 7, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 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]
2:42 pm on Jan 9, 2011 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 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
4:26 pm on Jan 9, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 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]
4:58 pm on Jan 9, 2011 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 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.
5:07 pm on Jan 9, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 0


Here is my one url example:

[sub-domain-name.com...] to be replaced phrase
5:11 pm on Jan 9, 2011 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 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
5:34 pm on Jan 9, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 15, 2010
posts: 126
votes: 0


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