Welcome to WebmasterWorld Guest from 54.198.93.179

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 to all lowercase characters

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

WebmasterWorld Senior Member 5+ Year Member



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)

5+ Year Member



Maybe this helps:

RewriteEngine On
RewriteMap lc int:tolower
...


Regards - Jonas
7:19 am on May 3, 2011 (gmt 0)

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



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:

<?php
$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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month