Using Apache htaccess file to change URL to lowercase

前端 未结 3 1819
一向
一向 2021-01-14 08:36

How can I modify my .htaccess file on Apache to do the following:

\"If the URL ends in .aspx, rewrite the entire URL to lowercase.\"

Backstory: I recently mi

3条回答
  •  误落风尘
    2021-01-14 08:54

    If you are not on a shared hosting environment and happy to enter the rules directly into your Apache configuration you can use mod_rewrites RewriteMap directive to do the lowercase conversion:

    RewriteMap lc int:tolower
    RewriteRule (.*?[A-Z]+.*) ${lc:$1} [R]
    

    For more information on this see the Apache manual: Redirect a URI to an all-lowercase version of itself. Although it is noted there that it is recommended to use mod_speling instead of this rewrite rule.

    with Apache rewrite and PHP: Taken from here

    .htaccess

    RewriteEngine on
    RewriteBase /
    
    # force url to lowercase if upper case is found
    RewriteCond %{REQUEST_URI} [A-Z]
    # ensure it is not a file on the drive first
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url=$1 [QSA,L]
    

    rewrite-strtolower.php

    Setup up the rewrite module
    Check if the incoming URL contains any uppercase letters
    Ensure that the incoming URL does not refer to a file on disk (you may want to host a file with upper case letters in its name - something like a PDF file that a client has uploaded through the CMS you have supplied them for instance)
    Send all the requests that match aforementioned rules are then rewritten to our script that will do the actual conversion to lowercase work.
    The only thing to note here is the QSA modifier, which makes sure all the GET "variables" are passed onto the script

    Next up is the little snippet of PHP that does all the work!
    This is a file called rewrite-strtolower.php in the same directory as your .htaccess file mentioned above.

提交回复
热议问题