Mike
Mike

Reputation: 605

How to redirect uppercase url query string to lowercase in PHP

I recently changed the URL parameters in my query strings on a site I'm developing and need a efficient way in PHP to redirect all search engine indexed links that have query strings in them from an uppercase format to a lowercase format.

Here is a example:

http://computerhelpwanted.com/jobs/?occupation=Director&position=IT+Director

Notice how the query string parameters have the first letter in uppercase, except the word IT is all uppercase. I need to redirect to the lowercase version of the URL which is now what the site uses.

I tried out a htaccess + php work around but it is causing too many issues. Here's the link if you're interested in seeing that script. https://www.simonholywell.com/post/2012/11/force-lowercase-urls-rewrite-php.html as well as the question on stackoverflow when I was having issues with it PHP htaccess Redirect url with query string from uppercase to lowercase

So for this question, if possible I just want to stick with a pure PHP way of doing this. Unless you know of a way in htaccess without using httpd.conf or mapping or mod spelling because I do not have access to that.

All help is appreciated!

Upvotes: 2

Views: 3602

Answers (2)

georg
georg

Reputation: 214969

In pure PHP,

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

If you want a permanent redirect:

header(above thing, true, 301);

Upvotes: 2

drmad
drmad

Reputation: 630

You can try something like:

if ( $_SERVER['REQUEST_URI'] != strtolower ( $_SERVER['REQUEST_URI'] ) )
    header ('Location: //' . $_SERVER['HTTP_HOST'] . strtolower ( $_SERVER['REQUEST_URI'] ));

This will redirect to the lowercase version of the URI only if it is not already in lower case. The // at the start is replaced by the browser to the actual url schema.

Upvotes: 4

Related Questions