homepage Welcome to WebmasterWorld Guest from 54.167.174.90
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

    
301 redirects for htm Pages
Redirects to fix uppercase/lowercase errors in URLs
hypasha

5+ Year Member



 
Msg#: 3121118 posted 4:32 pm on Oct 14, 2006 (gmt 0)

I moved my site from a windows dedicated server to a unix one. ALl is a mess I need to do 301 redirects and have tried a few but did not work...how do I redirect a htm page on a unix apache server?
Also where do I put the code at?

 

ahmedtheking

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3121118 posted 8:28 pm on Oct 15, 2006 (gmt 0)

Right, first have a read of this:

[httpd.apache.org...]

And then, this is what you need:

[httpd.apache.org...]

jtara

WebmasterWorld Senior Member jtara us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3121118 posted 12:31 am on Oct 16, 2006 (gmt 0)

Welcome to Redirect World! ;)

hypasha

5+ Year Member



 
Msg#: 3121118 posted 3:59 pm on Oct 16, 2006 (gmt 0)

OMG! and triple arrrgs and huh? lol..read thru information...gads...am clueless is all way over my head....now only hope host will guide me..or will have to find another dedicated server...what a nightmare this has been...meanwhile google cannot find 2000 of my pages...and the dupes I made have been knocked down in ranking....lol

thank you for response and links...made me realize I have less of a clue than I thought..:) Onward....................

g1smd

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



 
Msg#: 3121118 posted 9:44 pm on Oct 16, 2006 (gmt 0)

What do you need to redirect, and from where to where?

A whole site to another domain, but with the same folder and file names?

A folder moved to another folder, but with same file names?

Explain with "example" names (not the real ones).

hypasha

5+ Year Member



 
Msg#: 3121118 posted 4:46 am on Oct 17, 2006 (gmt 0)

need to change files mostly cap errors..so Blue-Dot-Widgets-1.htm and Blue-dot-Widgets-1.htm

to blue-dot-widgets-1.htm

I have many....

I do have root access (yes scarey thought..;)

jdMorgan

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



 
Msg#: 3121118 posted 4:01 pm on Oct 17, 2006 (gmt 0)

It's good that you have config access, because doing case-changes in .htaccess is very inefficient and slow.

See the example case-conversion code in the RewriteMap section of the Apache mod_rewrite documentation; It makes use of the operating system's "tolower" function, and is quite efficient compared to lowercasing characters one-at-a-time, as required in .htaccess.

Jim

g1smd

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



 
Msg#: 3121118 posted 6:46 pm on Oct 17, 2006 (gmt 0)

As well as setting up the redirects, work towards getting all internal links to point to all-lower-case versions of those URLs too.

hypasha

5+ Year Member



 
Msg#: 3121118 posted 2:18 am on Oct 18, 2006 (gmt 0)

the host tired the rewrite it seemed to work except home page would no longer work...
this is from them:
The rewrite thing you wanted me to add rewrites all upper case URLs including the filename to lowercase. So if you typed in or linked to [widgets.com...] it would redirect it to [widgets.com...] When the code was added it seemed to work like it was supposed to but for some reason the index page could not be loaded if you went directly to widgets.com. widgets.com/index.htm or widgets/Index.htm both worked. So the site was still completely up but the homepage would not load. I have removed the code. The only solution is going to be to fix the links.

FromRocky

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3121118 posted 3:07 am on Oct 18, 2006 (gmt 0)

So if you typed in or linked to [widgets.com...] it would redirect it to [widgets.com...] When the code was added it seemed to work like it was supposed to but for some reason the index page could not be loaded if you went directly to widgets.com

From the code, TEST.HTM and test.htm are the same but their urls are difference. This is a reason why they're in the loop and the page can't be loaded if the code was added to redirect TEST.THM to test.htm.

Is there anyone know how to redirect TEST.HTM ot Test.htm to test.htm using 301 without going into a loop?

jdMorgan

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



 
Msg#: 3121118 posted 4:56 am on Oct 18, 2006 (gmt 0)

There are three mod_rewrite solutions for the generic uppercase-to-lowercase URL conversion problem: A relatively-efficient solution with a RewriteMap and RewriteRule in httpd.conf, conf.d, or other server configuration file, a relatively-efficient solution with a RewriteMap defined in httpd.conf, conf.d, or other server config file, but called by a RewriteRule located in an .htaccess file, and a very-inefficient solution for use in .htaccess-only which replaces uppercase characters one-by-one:

# Solution using a RewriteMap and a RewriteRule in httpd.conf, conf.d, etc.
#
RewriteMap LowerCaseIt int:tolower
RewriteRule ^/([^A-Z]*[A-Z].*)$ http://www.example.com/${LowerCaseIt:$1} [R=301,L]

-----

# Solution using a RewriteMap defined in httpd.conf, conf.d, etc. (as
# shown in the example above), and a RewriteRule located in .htaccess
#
RewriteRule ^([^A-Z]*[A-Z].*)$ http://www.example.com/${LowerCaseIt:$1} [R=301,L]

-----

# .htaccess-only solution
#
# If no uppercase characters in current URL-path, skip next 28 rules
RewriteRule ![A-Z] - [S=28]
#
# Else replace one instance of each uppercase letter present
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2
#
# Set the redirect-required flag since at least one
# uppercase letter must have been replaced to get here
RewriteRule . - [E=Redirect:Yes]
#
# If any uppercase letters remain in the URL-path,
# then restart the mod_rewrite code from the top
RewriteRule [A-Z] - [N]
#
# (Skip from [S=28] flag above lands here)
# If the redirect-required flag has been set, invoke an external
# 301 redirect to notify the client that the URL has been changed
RewriteCond %{ENV:Redirect} ^Yes$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

If you have server config file access, I strongly suggest you use either the first or second solution; Use the .htaccess-only solution only if you must -- for example, if you're on shared hosting with no access to the server config files.

If you must use the .htaccess-only solution, be aware that if a URL contains multiple instances of any given uppercase character, the code will restart mod_rewrite processing to replace subsequent instances of that character. So to minimize execution time, place the code as close as you practically can to the top of your existing mod_rewrite code without interfering with its function, and place the mod_rewrite code as close to the top of your .htaccess file as you can without interfering with its structure.

If you have one file or directory (or a few files or directories) that must be named with uppercase letters, you won't want this code to redirect that URL (or those URLs). You can add a RewriteRule above this code to skip it if that URL is requested. For example, add

RewriteRule ^my-required-uppercase-URL\.html$ - [S=30]

above the .htaccess example to skip the whole thing if /my-required-uppercase-URL.html is requested.

I just typed this code. It is untested and comes with no warranty, so please test it thoroughly and then use it at your own risk.

Jim

[edited by: jdMorgan at 1:14 am (utc) on Oct. 19, 2006]

jdMorgan

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



 
Msg#: 3121118 posted 1:41 am on Oct 19, 2006 (gmt 0)

I tested the .htaccess version of the code above, and noticed that on Apache 1.x, it triggers a very nasty old mod_rewrite bug [archive.apache.org].

So, after some tinkering, I found a work-around that functions on Apache 1.3. It is very much a kludge, but it seems to work.

# Uppercase-to-lowercase URL redirect - .htaccess-only solution
#
# Modified with work-around for Apache 1.3 mod_rewrite path bug
# (See http://archive.apache.org/gnats/7879)
#
# If no uppercase characters in current URL-path, skip next 28 rules
RewriteRule ![A-Z] - [S=28]
#
# Else replace first instance of each uppercase letter present
RewriteRule ^([^A]*)A([^<]*) $1a$2<
RewriteRule ^([^B]*)B([^<]*) $1b$2<
RewriteRule ^([^C]*)C([^<]*) $1c$2<
RewriteRule ^([^D]*)D([^<]*) $1d$2<
RewriteRule ^([^E]*)E([^<]*) $1e$2<
RewriteRule ^([^F]*)F([^<]*) $1f$2<
RewriteRule ^([^G]*)G([^<]*) $1g$2<
RewriteRule ^([^H]*)H([^<]*) $1h$2<
RewriteRule ^([^I]*)I([^<]*) $1i$2<
RewriteRule ^([^J]*)J([^<]*) $1j$2<
RewriteRule ^([^K]*)K([^<]*) $1k$2<
RewriteRule ^([^L]*)L([^<]*) $1l$2<
RewriteRule ^([^M]*)M([^<]*) $1m$2<
RewriteRule ^([^N]*)N([^<]*) $1n$2<
RewriteRule ^([^O]*)O([^<]*) $1o$2<
RewriteRule ^([^P]*)P([^<]*) $1p$2<
RewriteRule ^([^Q]*)Q([^<]*) $1q$2<
RewriteRule ^([^R]*)R([^<]*) $1r$2<
RewriteRule ^([^S]*)S([^<]*) $1s$2<
RewriteRule ^([^T]*)T([^<]*) $1t$2<
RewriteRule ^([^U]*)U([^<]*) $1u$2<
RewriteRule ^([^V]*)V([^<]*) $1v$2<
RewriteRule ^([^W]*)W([^<]*) $1w$2<
RewriteRule ^([^X]*)X([^<]*) $1x$2<
RewriteRule ^([^Y]*)Y([^<]*) $1y$2<
RewriteRule ^([^Z]*)Z([^<]*) $1z$2<
#
# Set the redirect-required flag since at least one
# uppercase letter must have been replaced to get here
RewriteRule . - [E=Redirect:Yes]
#
# If any uppercase letters remain in the URL-path,
# then restart the mod_rewrite code from the top
RewriteRule [A-Z][^<]*< - [N]
#
# (Skips from the [S=28] flag above land here)
# If the redirect-required flag has been set, invoke an external
# 301 redirect to notify the client that the URL has been changed
RewriteCond %{ENV:Redirect} ^Yes$
RewriteRule ^([^<]+) http://www.example.com/$1 [R=301,L]

All of the stuff related to appending and stripping the "<" characters has to do with the mod_rewrite bug work-around. Basically, I am demarcating the 'real' end of the current URL, and then stripping any additional path info appended by the bug. See the link above for information on the bug itself, since none of this will make much sense without reading the bug report.

It's unlikely, but if your URLs (not including any query strings) include a "<" character, then you must use a different demarcation character, taking care that the character you choose is not a special regular-expressions token. This leaves only a few choices, so I'd recommend using "~" (tilde), ";", ">", or "<" as shown.

Jim

FromRocky

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3121118 posted 2:03 am on Oct 19, 2006 (gmt 0)

Thanks, Jim

hypasha

5+ Year Member



 
Msg#: 3121118 posted 5:36 pm on Oct 19, 2006 (gmt 0)

Thank you....will read bugs...but to tell you the truth am afraid to try anymore.....as everytime host has tried something we go down...they tried soemthing after we moved my sites over and was over a week before they could get server back up..hence ..my fears..:)But I am going to read thru this and try to learn more...hoepfully gain some confidence..thank you so very much for all of the work you did..greatly appreciated.

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