homepage Welcome to WebmasterWorld Guest from
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 to all lowercase characters

WebmasterWorld Senior Member 5+ Year Member

Msg#: 4304536 posted 12:47 am on Apr 27, 2011 (gmt 0)

Hey guys,

I'm helping a friend who's converting his blog from Joomla to Wordpress.

We've managed to do it all, but one part. The Joomla url's are a mix of uppercase and lowercase letters. Wordpress urls are all lowercase.

So we want to create a rule, that will 301 redirect any combination of upper/lower case chars to the lowercase url. We'll be using htaccess to achieve it.

Besides the odd 301 redirect, I'm not well versed with htaccess.

Is this possible? and if so, do you know the rules we'll need?



5+ Year Member

Msg#: 4304536 posted 3:42 am on May 3, 2011 (gmt 0)

Maybe this helps:

RewriteEngine On
RewriteMap lc int:tolower

Regards - Jonas


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

Msg#: 4304536 posted 7:19 am on May 3, 2011 (gmt 0)

You can redirect any request with one or more upper-case characters using mod_rewrite in .htaccess, but it is far easier to do it using PHP. Here's a rough idea as to what to do.

The upper-case "hook" in .htaccess should be one of the very first rules:

RewriteRule [A-Z]+ /my-special-script.php [L]

This is one of the rare occasions where a rewrite will be listed in .htaccess BEFORE the redirects. Normally we would want to avoid that, with rewrites AFTER redirects. However, when the PHP script is going to do all the work, the rewrite must be right at the beginning of all of the rules.

The chunk of PHP code that fixes the URL and sends the redirect response looks like this:

$server_url = $_SERVER['HTTP_HOST'];
IF (preg_match('/^www\./', $server_url)!==true)
{$server_url = "www." . $server_url; };
$lower_url = strtolower('http://' . $server_url . $_SERVER['REQUEST_URI']);
HEADER "Status: HTTP/1.1 301 Moved Permanently";
HEADER "Location: " . $lower_url;

The above PHP code might have some syntax errors in it, but that's the basic start.

You can do any test and any manipulation you like in the PHP code. It's one of the few occasions when it is better to do the work in PHP, not in .htaccess.

The other redirects should be done in .htaccess but only for lower case URLs. Upper case URLs will be detected and fixed up by the PHP script because the upper-case-detecting-rule is the first rule in the .htaccess file.

At this point I am also going to recommend that you completely ditch extensions for HTML pages (you need to keep them for images, and stylesheets) and go extensionless. It makes working with .htaccess, and rewrites and redirects in general, even easier.

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