Jayson Tamayo
Jayson Tamayo

Reputation: 2811

Convert a single character in the URL during htaccess Rewrite

I would like to convert a single lowercase letter into an uppercase letter during rewrite in .htaccess. For example, I have this

URL: www.domain.com/?country=CUoatia

and I wanted it to become: www.domain.com/Croatia

In addition, the uppercase letter 'U' will be converted to 'r'.

I can achieve www.domain.com/CUoatia using the following code:

RewriteCond %{QUERY_STRING} ^country=(.*)
RewriteRule ^$ %1? [R=301]

But I do not know how to change that 'U' character to 'r'.

Upvotes: 0

Views: 1000

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You can try changing your rules around to:

# Replace all capital U in query string
RewriteCond %{QUERY_STRING} ^(.*)U(.*)$
RewriteRule ^(.*)$ /$1?%1r%2  [L]

# Make sure there are no capital U in the query string
RewriteCond %{QUERY_STRING} !U
RewriteCond %{QUERY_STRING} ^country=(.*)
RewriteRule ^$ %1? [R=301]

Upvotes: 1

Related Questions