Server version: Apache / 2.4.7 (Ubuntu).

Friends, how is it possible at the level of htaccess links where the parameters contain the upper case redirect (301 redirect) to the same lower case links?

I am trying to implement as follows:

RewriteCond %{REQUEST_URI} [AZ] RewriteRule ^(.*)$ /${lc:$1} [R=301,L] 

But so far it does not work.

2 answers 2

And why do you have to do it at the .htaccess level?

I have repeatedly encountered the fact that the attempt to convert registers via .htaccess led to the fact that the corresponding Apache process was eating out all the memory that could reach. I don’t know what it was connected with, but I myself observed such a problem and came across descriptions on the Web.

As an alternative, I have come across such a rather smart method, which is much less expensive in memory: https://www.simonholywell.com/post/2012/11/force-lowercase-urls-rewrite-php/

Its essence is that requests that contain uppercase and are not a file on disk are redirected to a special PHP script:

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

And the file itself contains the following code:

 <?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('Unable to convert the URL to lowercase. You must supply a URL to work upon.'); 

You can also try using the mod_speling module. As stated in the documentation: "If, after scanning the directory, ... only one document is found that "almost" matches the request, then it is returned in the form of a redirection response" If all you need is case conversion - enable CheckCaseOnly on directive.


    Add a record to the virtual host file:

     RewriteMap lc int:tolower 

    After that, we add the following entries to .htaccess:

     RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^[^AZ]*[AZ].* RewriteRule ^ ${lc:%{REQUEST_URI}} [L,R=301] 

    The second option is similar to the rules:

     RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} [AZ] RewriteRule (.*) ${lc:$1} [R=301,L] 

    Method 2 is the most cumbersome, but does not require access to the Apache settings. Just add the rules in .htaccess:

     RewriteEngine On RewriteBase / # If there are caps, set HASCAPS to true and skip next rule RewriteRule [AZ] - [E=HASCAPS:TRUE,S=1] # Skip this entire section if no uppercase letters in requested URL RewriteRule ![AZ] - [S=28] # Replace single occurance of CAP with cap, then process next Rule. RewriteRule ^([^A]*)A(.*)$ $1a$2 RewriteRule ^([^B]*)B(.*)$ $1b$2 RewriteRule ^([^C]*)C(.*)$ $1c$2 RewriteRule ^([^D]*)D(.*)$ $1d$2 RewriteRule ^([^E]*)E(.*)$ $1e$2 RewriteRule ^([^F]*)F(.*)$ $1f$2 RewriteRule ^([^G]*)G(.*)$ $1g$2 RewriteRule ^([^H]*)H(.*)$ $1h$2 RewriteRule ^([^I]*)I(.*)$ $1i$2 RewriteRule ^([^J]*)J(.*)$ $1j$2 RewriteRule ^([^K]*)K(.*)$ $1k$2 RewriteRule ^([^L]*)L(.*)$ $1l$2 RewriteRule ^([^M]*)M(.*)$ $1m$2 RewriteRule ^([^N]*)N(.*)$ $1n$2 RewriteRule ^([^O]*)O(.*)$ $1o$2 RewriteRule ^([^P]*)P(.*)$ $1p$2 RewriteRule ^([^Q]*)Q(.*)$ $1q$2 RewriteRule ^([^R]*)R(.*)$ $1r$2 RewriteRule ^([^S]*)S(.*)$ $1s$2 RewriteRule ^([^T]*)T(.*)$ $1t$2 RewriteRule ^([^U]*)U(.*)$ $1u$2 RewriteRule ^([^V]*)V(.*)$ $1v$2 RewriteRule ^([^W]*)W(.*)$ $1w$2 RewriteRule ^([^X]*)X(.*)$ $1x$2 RewriteRule ^([^Y]*)Y(.*)$ $1y$2 RewriteRule ^([^Z]*)Z(.*)$ $1z$2 # If there are any uppercase letters, restart at very first RewriteRule in file. RewriteRule [AZ] - [N] RewriteCond %{ENV:HASCAPS} TRUE RewriteRule ^/?(.*) /$1 [R=301,L]