Search code examples
regexapache.htaccessmod-rewrite

Convert first letter to uppercase after a specific character URLs using htaccess


I have a URL that looks like site.com/test+test and there is a rule in htaccess file that converts this url to site.com/Test-Test

RewriteEngine On

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

where the first RewriteRule line replaces the first letter with a capital letter after the "/" and does this only after the first 2 "/" characters encountered in the URL, and the second RewriteRule line finds all the "+" characters in the URL and replaces them with "-" as well changes lowercase letter to uppercase but the question is how to make the second rule work only 2 times as the first rule(two times after first / and second /, symbol +; the "+" symbol can appear more than once in the first and second cases after the "/")

example: we have url: site.com/test+test/test2+test2/test3+test3

after changing the URl it should look like this: site.com/Test-Test/Test2-Test2/test3-test3

in my case, the code for changing the URL works like this: site.com/Test-Test/Test2-Test2/test3-Test3 where test3 after / does not increase the letter correctly and after - the letter increases by mistake


Solution

  • xample: we have url: site.com/test+test/test2+test2/test3+test3

    after changing the URl it should look like this: site.com/Test-Test/Test2-Test2/test3-test3

    You may use this rule to handle this:

    RewriteRule ^([A-Z][^/]+/)?([a-z])([^+]*)\+([a-z])(.*) /$1${uc:$2}$3-${uc:$4}$5 [R=302,L]