PHP htaccess的重定向URL与查询字符串从大写改为小写 [英] PHP htaccess Redirect url with query string from uppercase to lowercase

查看:517
本文介绍了PHP htaccess的重定向URL与查询字符串从大写改为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在htaccess的这个小PHP脚本和几行重定向的URL从大写字母查询字符串为小写。

然而,它重定向大写字符查询字符串为小写的人只有存在于URL的url文件或目录的部分大写字母开头。

示例用大写的目录:

  domain.com/JOBS/?position=Java+Developer
 

将被重定向到

  domain.com/jobs/?position=java+developer
 

示例没有大写的目录或文件名,但只有在查询字符串:

  domain.com/jobs/?position=Java+Developer
 

将被重定向到

  domain.com/jobs/?position=Java+Developer
 

第一个例子成功重定向目录和查询字符串全部小写。

第二个例子并不重定向的查询字符串为小写,它仍然是相同的。

我想不出在code更改,以获得查询字符串重定向到小写不管是什么,如果目录或文件名是大写还是不行。

下面是code:

的htaccess

  RewriteEngine叙述上

的RewriteBase /

#强制URL为小写,如果大写被发现
的RewriteCond%{REQUEST_URI} [A-Z]
重写规则(。*)改写-strtolower.php?重写,用strtolower-URL = $ 1 [QSA,L]
 

PHP脚本

 < PHP
如果(使用isset($ _ GET ['重写-用strtolower-URL'])){
    $ URL = $ _GET ['重写-用strtolower-URL'];
    取消设置($ _ GET ['重写-用strtolower-URL']);
    $ PARAMS = http_build_query($ _ GET);
    如果(strlen的($ params)方法){
        $ PARAMS =? 。用strtolower($ params)方法;
    }
    头('位置:http://'$ _ SERVER ['HTTP_HOST']'/'用strtolower($ URL)$参数,可以真实的,301。);
    出口;
}
标题(HTTP / 1.0 404未找​​到);
死亡(无法将URL转换为小写,您必须提供在工作的URL。');
?>
 

解决方案

相反的:

  $ PARAMS = http_build_query($ _ GET);
 

使用这样的:

  $ PARAMS =用strtolower(http_build_query($ _ GET));
 

和你的.htaccess应该是:

  RewriteEngine叙述上
的RewriteBase /

#强制URL为小写,如果大写被发现
的RewriteCond%{REQUEST_URI} [A-Z] [OR]
的RewriteCond%{QUERY_STRING} [A-Z]
重写规则(。*)改写-strtolower.php?重写,用strtolower-URL = $ 1 [QSA,L,NE]
 

既然你应该叫你的PHP处理程序,这两种情况。

I have this small php script and couple lines in htaccess to redirect urls with query strings from uppercase to lowercase.

However, it redirects uppercase characters in query string to lowercase ones only if there is an uppercase character in the url file or directory part of the url.

Example with uppercase directory:

domain.com/JOBS/?position=Java+Developer

will be redirected to

domain.com/jobs/?position=java+developer

Example with no uppercase in directory or file name, but only in query string:

domain.com/jobs/?position=Java+Developer

will be redirected to

domain.com/jobs/?position=Java+Developer

The first example successfully redirects the directory and the query string to all lowercase.

The second example does not redirect the query string to lowercase, it remains the same.

I can't figure out what to change in the code to get the query string to redirect to lowercase no matter if the directory or file name is uppercase or not.

Here is the code:

htaccess

RewriteEngine On

RewriteBase /

# force url to lowercase if upper case is found
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url=$1 [QSA,L]

PHP script

<?php
if(isset($_GET['rewrite-strtolower-url'])) {
    $url = $_GET['rewrite-strtolower-url'];
    unset($_GET['rewrite-strtolower-url']);
    $params = http_build_query($_GET);
    if(strlen($params)) {
        $params = '?' . strtolower($params);
    }
    header('Location: http://' . $_SERVER['HTTP_HOST'] . '/' . strtolower($url) . $params, true, 301);
    exit;
}
header("HTTP/1.0 404 Not Found");
die('Unable to convert the URL to lowercase. You must supply a URL to work upon.');
?>

解决方案

Instead of:

$params = http_build_query($_GET);

use it like this:

$params = strtolower ( http_build_query($_GET) );

And your .htaccess should be:

RewriteEngine On
RewriteBase /

# force url to lowercase if upper case is found
RewriteCond %{REQUEST_URI} [A-Z] [OR]
RewriteCond %{QUERY_STRING} [A-Z]
RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url=$1 [QSA,L,NE]

Since you should be calling your PHP handler for both the cases.

这篇关于PHP htaccess的重定向URL与查询字符串从大写改为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆