Home » Tutorials » PHP/MySQL » Force URLs to lowercase with htaccess and PHP

Force URLs to lowercase with htaccess and PHP

htaccess wallpaper

When optimizing your website (SEO) you must check if your website URLs load both with lowercase and uppercase letters. For example mysite.com/My-URL and mysite.com/my-url. It may even be mixed. This is bad and if you don’t fix it, it may harm your website optimization.

The solution is to force browsers to redirect all URLs to the lowercase version. The Australian developer Simon Holywell gives us great solution for this in his blog, but the problem is that he forgot one important issue.

Here is the code:

In your projects .htaccess file, you will need to add the following rewrite rules.


RewriteEngine on
RewriteBase /

# force url to lowercase if upper case is found
RewriteCond %{REQUEST_URI} [A-Z]
# ensure it is not a file on the drive first
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule (.*) strtolower.php?rewrite-strtolower-url=$1 [QSA,L]

The only problem with this script is that it breaks the images and other case sensitive files. Like directory names. If the image URL is example.com/Files/Image1.jpg it makes it example.com/files/image1.jpg, so it doesn’t load anymore.

The solution is to insert after

RewriteBase /

insert this code here

RewriteCond %{REQUEST_URI} !(images|media|image|thumbs) [NC]

This says not to rewrite folders named images,media, image or thumbs.

The PHP code

The next step is to create PHP file, which does everything. Open some text editor (for example notepad), paste the below code and save it as strtolower.php. Then upload it in the same directory as the .htaccess file.

<?php
if(isset($_GET['rewrite-strtolower-url'])) {
    $url = $_GET['rewrite-strtolower-url'];
    unset($_GET['rewrite-strtolower-url']);
    $params = http_build_query($_GET);
    if(strlen($params)) {
        $params = '?' . $params;
    }
    header('Location: http://' . $_SERVER['HTTP_HOST'] . '/' . strtolower($url) . $params, true, 301);
    exit;
}
header("HTTP/1.0 404 Not Found");
die('Error! Unable to convert the URL to lowercase.');
?>

After you do this, try to type your website url adress with uppercase letters and check if the redirect works.

Alternative method

If you are not on a shared hosting and know how to enter the rules directly into your Apache configuration you can use mod_rewrites RewriteMap directive to do the conversion to lowercase. You need to edit the server/vhost config files (httpd.conf).

RewriteMap lc int:tolower
RewriteRule (.*?[A-Z]+.*) ${lc:$1} [R]

If you have any comments or advices, you can share them below the article.

When optimizing your website (SEO) you must check if your website URLs load both with lowercase and uppercase letters. For example mysite.com/My-URL and mysite.com/my-url. It may even be mixed. This is bad and if you don't fix it, it may harm your website optimization. The solution is to force browsers&hellip;

0%

User Rating: 4.7 ( 3 votes)
0

About Georgi Dyulgerov

Georgi Dyulgerov

Founder of fTutorials.com - the website aiming to keep you in touch with the latest technologies in the brand. Also a Freelance web developer.

3 comments

  1. These is nice information, I really appreciate content, which is cover over here, Fantastic.

    Thank You

  2. hey, this code works, BUT after applying it customers cant log in… any ideas why?

    regards,

Leave a Reply

Your email address will not be published. Required fields are marked *


Loading Facebook Comments ...