How do you use HTACCESS to set and 301 UPPER to lower case?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you use HTACCESS to set and 301 UPPER to lower case?

Post by simonmlewis »

Code: Select all

RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
We need to do 301s so that any URL in uppercase, 301s to lowercase. This is the script we have found that could do it, but not sure about the conf file with the "lc".

Is there no other simple htaccess way?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you use HTACCESS to set and 301 UPPER to lower ca

Post by Celauran »

RewriteMap needs to go in your virtual host definition, not your .htaccess
While you cannot declare a map in per-directory context (.htaccess files or <Directory> blocks) it is possible to use this map in per-directory context.
https://httpd.apache.org/docs/current/r ... temap.html
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you use HTACCESS to set and 301 UPPER to lower ca

Post by Celauran »

However, this would be trivial to implement in PHP.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use HTACCESS to set and 301 UPPER to lower ca

Post by simonmlewis »

Is it important to do this - because we have pages that are apparently cached by Google with CAPITALS, and the same with lowercase.
So should we be finding a way to 301 to lowercase?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use HTACCESS to set and 301 UPPER to lower ca

Post by simonmlewis »

Code: Select all

<font color='#ffffff'>
<?php
$uppercaseurlcheck = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$pos = strcspn($uppercaseurlcheck, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');
if ($pos < strlen($string)) {
    echo "capital letter as index $pos";
}
else { echo "nothing caps";}
 ?></font>
Hi

I'm am sort of getting there. Using this as an include file just to test it. But it's erroring on line six,

Code: Select all

if ($pos < strlen($string)) {
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you use HTACCESS to set and 301 UPPER to lower ca

Post by Celauran »

Seems unnecessarily complex.

Convert the request to lower case. Compare against the original request. If original is different (ie. not lower case), redirect.

Code: Select all

<?php

$request_uri = $_SERVER['REQUEST_URI'];
if (strtolower($request_uri) !== $request_uri) {
    // redirect
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you use HTACCESS to set and 301 UPPER to lower ca

Post by simonmlewis »

Code: Select all

$uppercaseurlcheck = $_SERVER['REQUEST_URI'];
$converttolower = strtolower($uppercaseurlcheck);

if(preg_match('/[A-Z]/', $uppercaseurlcheck)){
      header("Location: http://site.co.uk" . $converttolower,TRUE,301);
      exit();
}
This is what I opted for before I saw your message. Similar?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you use HTACCESS to set and 301 UPPER to lower ca

Post by Celauran »

Pretty much the same thing, yeah
Post Reply