Case-insensitive htaccess 301

11,904

Solution 1

Don't think there's a way to make the Redirect directive (part of mod_alias) case insensitive, but there's a mod_rewrite flag that you can use. You'll need to change all of your redirects from this:

 Redirect 301 /post/My-Blog-Post.aspx https://www.example.com/blog/a-new-post/

to:

 RewriteRule ^/?post/My-Blog-Post.aspx$ https://www.example.com/blog/a-new-post/ [L,R=301,NC]

Note the NC flag, meaning "no case". This will match any URI that looks like /post/my-blog-post.aspx and ignores case, and redirects it to https://www.example.com/blog/a-new-post/.

Solution 2

However, if you want to make "My-Blog-Post" a variable of "a-new-post", I mean if you want /post/My-Blog-Post.aspx to be redirected into /blog/a-new-post/ either /post/this.aspx into /blog/this/ or /post/that.aspx into /blog/that/ or even /post/anything.aspx into /blog/anything/, you can try to use this code in you .htaccess hidden file:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^post/([a-z0-9-_]+).aspx$ /blog/$1/ [R=301,NC]
Share:
11,904
developdaly
Author by

developdaly

Clean web design. Cleaner code. WordPress enthusiast. developdaly.com

Updated on July 19, 2022

Comments

  • developdaly
    developdaly almost 2 years

    I have about 300 redirects in the following format

    Redirect 301 /post/My-Blog-Post.aspx https://www.example.com/blog/a-new-post/

    That works great except the redirects are case sensitive and going to /post/my-blog-post.aspx doesn't redirect.

    There isn't a pattern for the old URL to new URL so this would likely have to be a flag for every redirect.

    How can I get the URL to redirect no matter the case?

  • developdaly
    developdaly about 11 years
    No dice -- same result.
  • serraosays
    serraosays over 10 years
    Adding the NC flag worked on a couple of different sites for me, one server running Apache 2.2 and another on 2.4. Check Apache's documentation, it is consistent with Jon Lin's answer: httpd.apache.org/docs/current/rewrite/flags.html#flag_nc