Convert first letter from lowercase to uppercase URLs using htaccess

Viewed 184

I want to convert example.com/test/test2 to example.com/Test/Test2. Using .htaccess or apache (2.4) rewritemap.

I found a lot of options, but they all change all the letters in the word, how to increase only the first letter after each "/"

Will really appreciate your kind answers.

I have tried following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond expr "toupper(%{REQUEST_URI}) =~ m#(.+)/?$#"
RewriteRule [a-z] %1/ [L]
1 Answers

I found a lot of options, but they all change all the letters in the word, how to increase only the first letter after each "/"

First define this RewriteMap in your Apache config and restart the web server.

RewriteMap uc int:toupper

Then use rule in your site root .htaccess to convert first letter after each / to uppercase.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Z].*/)?([a-z])(.*) /$1${uc:$2}$3 [R=302,L,NE]

Test it after clearing your browser cache.


Update: Based on comment below to uppercase only in the first two subdirectories use:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Z][^/]+/)?([a-z])(.*) /$1${uc:$2}$3 [R=302,L]
Related