Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 to all lowercase characters

12:47 am on Apr 27, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 9, 2007
votes: 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?
3:42 am on May 3, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 4, 2006
posts: 53
votes: 0

Maybe this helps:

RewriteEngine On
RewriteMap lc int:tolower

Regards - Jonas
7:19 am on May 3, 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
votes: 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.