Created
October 18, 2015 06:28
-
-
Save joshbuchea/ba40228fbd1f96e413b1 to your computer and use it in GitHub Desktop.
A function to redirect uppercase URLs to lowercase.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Changes the requested URL to lowercase. | |
* | |
* Only if URL does not include a filename or query variable. | |
*/ | |
function force_lowercase_urls() { | |
// Grab requested URL | |
$url = $_SERVER['REQUEST_URI']; | |
// If URL contains a period, halt (likely contains a filename and filenames are case specific) | |
if ( preg_match('/[\.]/', $url) ) { | |
return; | |
} | |
// If URL contains a question mark, halt (likely contains a query variable) | |
if ( preg_match('/[\?]/', $url) ) { | |
return; | |
} | |
if ( preg_match('/[A-Z]/', $url) ) { | |
// Convert URL to lowercase | |
$lc_url = strtolower($url); | |
// 301 redirect to new lowercase URL | |
header('Location: ' . $lc_url, TRUE, 301); | |
exit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@SinnlosS no good reason, this is just an old gist I created 5 years ago and probably didn't spend much time slapping together 😄
I have a free plugin in the WP marketplace with this same functionality and I may have resolved the issue there but I can't recall: