How would I make Apache case insensitive using .htaccess?

て烟熏妆下的殇ゞ 提交于 2019-12-05 11:13:15

问题


I recently switched from IIS to Apache and unfortionately some of my links have capitalization issues. I have seen quite a few people talking about how to rewrite urls to be all lowercase or all uppercase but I need something to just make Apache case insensitive. Is this doable with .htaccess?


回答1:


add

CheckSpelling on

to your .htaccess file of course after enabling the RewriteEngine

so the final code will be

RewriteEngine on
CheckSpelling on

I guess it is the best and safest way.

dont forget to change

AllowOverride none

to

AllowOverride All

inside your httpd.conf file, to allow .htaccess files to work correctly.




回答2:


If CheckSpelling isn't working for you and you're using PHP, you can add a PHP redirect in your 404 page to redirect to the lowercase version of the URL. To start, if you haven't already done so, add the following code to your .htaccess file (you can call the file something else than 404.php if you want, but it has to be a PHP file):

ErrorDocument 404 /404.php

Then add the following code at the beginning of 404.php (if you're using HTTPS, change http:// to https:// at the second line):

if(preg_match("/[A-Z]/", $_SERVER["REQUEST_URI"])){
    header("location:http://" . $_SERVER["HTTP_HOST"] . strtolower($_SERVER["REQUEST_URI"]));
    exit();
}

This code uses a simple regex to check if the requested URI contains uppercase letters, and if it does, it redirects to the same page but with lowercase letters.



来源:https://stackoverflow.com/questions/17223855/how-would-i-make-apache-case-insensitive-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!