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

Apache Web Server Forum

    
Replacing Underscores with Dashes, past 9
This is an old topic, I want to expand on it...
chopin2256

5+ Year Member



 
Msg#: 3874354 posted 3:30 pm on Mar 19, 2009 (gmt 0)

I am looking for variable replacements past 9. Let me first refer to this topic here:

[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?

 

jdMorgan

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



 
Msg#: 3874354 posted 3:33 pm on Mar 19, 2009 (gmt 0)

Use multiple chained rules.

Jim

chopin2256

5+ Year Member



 
Msg#: 3874354 posted 5:18 pm on Mar 19, 2009 (gmt 0)

Thanks for your response. I am not sure I fully understand what you mean. I have 11 rules, but I will only wrote down one rule where I start having trouble:


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?

chopin2256

5+ Year Member



 
Msg#: 3874354 posted 6:27 pm on Mar 19, 2009 (gmt 0)

I'd like to update my post. I was able to get at least 11 dashes replaced with underscores by using this code here:

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?

jdMorgan

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



 
Msg#: 3874354 posted 7:12 pm on Mar 19, 2009 (gmt 0)

You'll need to include "articles" in the new URL-path as well, or the rule will "drop it" from the new URL-path. You should also include "articles" in both the pattern and the substitution of the redirect rule for efficiency's sake:

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]

I should also note that the title of this thread may be misleading, since this redirect is actually replacing hyphens in the URL with underscores, not the other way around...

Jim

chopin2256

5+ Year Member



 
Msg#: 3874354 posted 7:22 pm on Mar 19, 2009 (gmt 0)

I understand the title is misleading, you can change it...I just figured that this is exactly like the "underscore to dash" thread, so I wanted to reference that..

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.

Caterham

10+ Year Member



 
Msg#: 3874354 posted 7:52 pm on Mar 19, 2009 (gmt 0)

Wait for 2.2.12 and the DPI flag or put the rules into your httpd.conf if you can or don't use the rule-pattern in subsequent rules (or rewrite to some perl, php or python script which replaces - with _ and returns the location header).

chopin2256

5+ Year Member



 
Msg#: 3874354 posted 7:57 pm on Mar 19, 2009 (gmt 0)

I can't wait, I need this ahora...I need to redirect a whole bunch of old wiki urls to my new subdomain. The above works perfectly fine using htaccess, except that oddity error. May I ask why you suggest not using the above to accomplish what I want? We are so close to cracking this!

jdMorgan

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



 
Msg#: 3874354 posted 8:40 pm on Mar 19, 2009 (gmt 0)

Unfortunately, this looks like the good old mod_rewrite bug [archive.apache.org] in Apache, where PathInfo gets re-injected into the substitution multiple times.

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]

What this does is to prevent the server URL-path variable from being rewritten more than once; All of the intermediate rewriting is done with and to the unscorURL variable instead.

It's very ugly, but it works.

Jim

[edited by: jdMorgan at 8:45 pm (utc) on Mar. 19, 2009]

chopin2256

5+ Year Member



 
Msg#: 3874354 posted 1:41 am on Mar 20, 2009 (gmt 0)

Doesn't matter how ugly it looks, as long as it does the job. This does exactly what I need, thank you for your patience and assistance!

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