简体   繁体   中英

how to change part of the url from capital to small letters after the directory path is changed apache/httpd

I want to be short and sweet. My website address was this https://www.example.com/ABCD/index.php

To make it convenient for users, I changed the ABCD directory to abcd (small case). Now users type https://www.example.com/abcd/index.php

However if users come to my site from google, there it is still cached as https://www.example.com/ABCD/index.php
and therefore they are not able to login.

I searched google and stack overflow and the closest effective I found is [here]. 1 ie

in http.conf

<IfModule mod_rewrite.c>
RewriteMap lc int:tolower
</IfModule>

and in .htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [R=301,L]

However it is also not working in my case. because it take users from https://www.example.com/ABCD/index.php?gene=MYH7
to
https://www.example.com/var/www/html/example.com/?gene=MYH7

Could anyone experienced please help me to correct this rule. All I want is to direct users to https://www.example.com/abcd/index.php
If they type
https://www.example.com/ABCD/index.php

Here is the .htaccess in my web root directory.

RewriteEngine On

# Added by Mian to direct all traffic to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.org%{REQUEST_URI} [NE,L,R]

# Added by Mian for url capital to small.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [R=301,L]

Change your rule with this one:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^ ${lc:%{REQUEST_URI}} [R=301,L,NE]

Changes:

  • Redirect path starts with a trailing slash. If trailing slash is missing then DocumentRoot path is appended by Apache as you're seeing in your redirect rule.
  • Avoid capturing value from RewriteRule as your .htaccess might be in a sub-directory.
  • %{REQUEST_URI} represents full path from site root

Make sure to use a new browser for testing.

It seems not to work inside a .htaccess. I have the following working in my Apache config:

<VirtualHost *:80>
 rewriteengine on
 RewriteMap lc int:tolower
 RewriteRule "^/([A-Z]*)/(.*)$"  "${lc:$1}/$2"  [R=301]
</VirtualHost>

The following rule will lowercase the first part of the URL as soon as there is one uppercase character.

RewriteRule "^/([^/]*[A-Z][^/]*)/(.*)$"  "${lc:$1}/$2"  [R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM