Forum Moderators: phranque
[webmasterworld.com...]
The only reason I started a new thread is because it wouldn't let me reply (it is an old thread)
My question is, how can I account for 10+ dash replacements? When I get past $9 and use the variable replacement $10, $11, $12 etc, my urls look like this:
www.example.com/....on_the_tenth_0and_1eleventh_2etc
How do you use variable replacements past 9?
RewriteRule ^articles/([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$ http://wiki.example.com/$1_$2_$3_$4_$5_$6_$7_$8_$9_$10 [R=301,L] It seems that you can't go past $9, otherwise the variables will start repeating. Can you help point me in the right direction?
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$ $1_$2_$3_$4_$5_$6_$7 [E=unscors:Yes]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$ $1_$2_$3_$4_$5 [E=unscors:Yes]
RewriteRule ^([^-]*)-([^-]*)-(.*)$ $1_$2_$3 [E=unscors:Yes]
RewriteRule ^([^-]*)-(.*)$ $1_$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] The only problem is, I cannot figure out how to get this to work if I only want pages in a directory to be rewritten, such as /articles. When I do this:
RewriteRule ^articles/([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$ $1_$2_$3_$4_$5_$6_$7 [E=unscors:Yes]
RewriteRule ^articles/([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$ $1_$2_$3_$4_$5 [E=unscors:Yes]
RewriteRule ^articles/([^-]*)-([^-]*)-(.*)$ $1_$2_$3 [E=unscors:Yes]
RewriteRule ^articles/([^-]*)-(.*)$ $1_$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] This code seems broken. Can anyone help?
Note also that if you wish to replace hyphens in URLs with N or fewer than N hyphens, then you will need enough rules to add up to at least N, but to replace any number fewer than N as well. As it works out, this can be done by including enough rules to replace up to 15 hyphens in your case. Put another way, you cannot "skip" any numbers between the minimum and maximum, so the series of rules may actually be able to replace more hyphens that the number you require.
RewriteRule ^articles/([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$ articles/$1_$2_$3_$4_$5_$6 [E=unscors:Yes]
RewriteRule ^articles/([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$ articles/$1_$2_$3_$4_$5 [E=unscors:Yes]
RewriteRule ^articles/([^-]*)-([^-]*)-([^-]*)-(.*)$ articles/$1_$2_$3_$4 [E=unscors:Yes]
RewriteRule ^articles/([^-]*)-([^-]*)-(.*)$ articles/$1_$2_$3 [E=unscors:Yes]
RewriteRule ^articles/([^-]*)-(.*)$ articles/$1_$2 [E=unscors:Yes]
#
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^articles/(.*)$ http://www.example.com/articles/$1 [R=301,L]
Jim
As for the above refined code, I still get a problem. Let's say my url has 5 dashes like so:
[example1.com...]
It will then redirect and rewrite to the following:
http://www.example.com/a_a_a_a_a_a/a_a_a_a_a_a/a_a_a_a_a_a/a-a-a-a-a-a/a-a-a-a-a-a/a-a-a-a-a-a
Notice I do want to redirect from /articles to / so I changed the last line of your code to:
RewriteRule ^articles/(.*)$ http://www.example.com/$1 [R=301,L]
So that part works fine. But the url still doesnt rewrite quite correctly. Very confusing.
Here's a work-around:
# Skip next seven rules if not a hyphenated "articles/" URL-path
RewriteRule !^articles/[^\-]*- - [S=7]
#
# Else get hyphenated "articles/" URL-path to user variable "unscorURL"
RewriteRule ^articles/([^\-]*-.*)$ - [E=unscorURL:$1]
#
# Replace hyphens with underscores in user variable
RewriteCond %{ENV:unscorURL} ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$
RewriteRule ^. - [E=unscorURL:%1_%2_%3_%4_%5_%6]
#
RewriteCond %{ENV:unscorURL} ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-(.*)$
RewriteRule ^. - [E=unscorURL:%1_%2_%3_%4_%5]
#
RewriteCond %{ENV:unscorURL} ^([^-]*)-([^-]*)-([^-]*)-(.*)$
RewriteRule ^. - [E=unscorURL:%1_%2_%3_%4]
#
RewriteCond %{ENV:unscorURL} ^([^-]*)-([^-]*)-(.*)$
RewriteRule ^. - [E=unscorURL:%1_%2_%3]
#
RewriteCond %{ENV:unscorURL} ^([^-]*)-(.*)$
RewriteRule ^. - [E=unscorURL:%1_%2]
#
# Externally redirect to replace hyphenated "articles" URL with underscored URL
RewriteRule ^. http://www.example.com/%{ENV:unscorURL} [R=301,L]
It's very ugly, but it works.
Jim
[edited by: jdMorgan at 8:45 pm (utc) on Mar. 19, 2009]