Welcome to WebmasterWorld Guest from 54.198.52.8

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

How to make a Case-insensitive url redirect to a case-sensitive url

How to make a Case-insensitive url redirect to a case-sensitive url

     
11:02 am on Aug 3, 2015 (gmt 0)

New User

joined:Aug 3, 2015
posts: 3
votes: 0


Need some help on writing a redirect rule or any thing to redirect a case-insensitive url to a case-sensitive url.

Ex:
https://example.com/webmaster/index.html to https://example.com/WebMaster/index.html

-> Which ever way the input comes like, webmaster or WEBMASTER or Webmaster or WEbMaster and so on should be redirected to WebMaster.
9:25 pm on Aug 3, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


You need a RewriteCond that says something like
%{REQUEST_URI} !exact-casing-you-want
And you'll need a separate rule for each URL you're redirecting. If there are many, you're better off doing it via a php-or-similar script. That's assuming this is not your own server, so you can't set up a RewriteMap.

<tangent>
Why do you feel you need this rule? Are a lot of outside sites linking to a lot of wrong forms of your URL? In most situations, it would be perfectly OK to let requests for WeBmAsTeR.hTmL go straight to a 404.
</tangent>

<further tangent>
I'm fond of CamelCase too, but it doesn't seem to be awfully popular in URLs right now.
</further tangent>
12:25 pm on Aug 6, 2015 (gmt 0)

New User

joined:Aug 3, 2015
posts: 3
votes: 0


Hello Lucy,

Thank you so much for the reply.

I have figured it out. It worked for me, below is what I have used.

RewriteEngine On
RewriteCond %{REQUEST_URI} !/WebMaster/(.*)$
RewriteRule ^/([a-zA-Zwebmaster]+)/(.*)$ /WebMaster/$2 [R=301,L]

Thanks again....
6:16 pm on Aug 6, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


Does that work? Ordinarily, RewriteRules in a directory context (whether .htaccess or a <Directory> section) can't begin with / slash.
10:11 pm on Aug 6, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


What Lucy24 said, plus:

RewriteRule ^/([a-zA-Zwebmaster]+)/(.*)$ /WebMaster/$2 [R=301,L]

The preceding says: Anything that starts with a / followed by a-zA-Z or any of the letters w-e-b-m-a-s-t-r individually [already covered by a-z], which matches anything a-zA-Z.

This is probably more what you're looking for:
RewriteCond %{REQUEST_URI} !/WebMaster/ 
RewriteRule ^/?webmaster/(.*) http://www.example.com/WebMaster/$1 [NC,R=301,L]

NC [flag] = No Case, so upper and lower are matched.

/? means if there is a / present at the start of the requested location or if there's not a / present at the start of the requested location.

webmaster outside of [ ] means webmaster, rather than any combination of w-e-b-m-a-s-t-r

http://www.example.com added to the right-side of the rule is a "best-practice", so you know exactly where the visitor is going every time and makes makes sure it generates an external redirect, even if you forget the R=NNN code.

* My version might need some adjustment, but I just got an "urgent fix" call, so that's all the time I have to spend thinking about this right now.
11:06 pm on Aug 6, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14036
votes: 522


Anything that starts with a / followed by a-zA-Z or any of the letters w-e-b-m-a-s-t-r individually [already covered by a-z], which matches anything a-zA-Z.

That will teach me to pounce on missing/spurious "not"s in your posts ;)

The target of an external redirect ("the right side") should always start with full protocol-plus-domain. If people are already getting your casing wrong, they're probably also making a mess of your domain name.

<tangent>
Even if you did have your RewriteRules lying loose in the config file, would a pattern ever start right out with / for the root? Aren't sites usually tucked away in subdirectories that would need to be included in the (physical) path?
</tangent>
2:47 pm on Aug 12, 2015 (gmt 0)

New User

joined:Aug 3, 2015
posts: 3
votes: 0


Thank you so much Lucy and TheMadScientist, yes my rule had some errors. I have used "/?" instead of [a-zA-Z....] and it works now. Thank you so much for your time.