【问题标题】:URL Rewriting with case insensitivity不区分大小写的 URL 重写
【发布时间】:2016-11-03 04:30:24
【问题描述】:

我想创建不区分大小写的 URL,我已经在使用 CheckSpelling On 并且它工作正常。 同时,我还希望从我申请的 URL 中删除扩展名

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

它也有效。 但两者都不完全奏效。 如果我将两者都保留在 htaccess 中,它会开始给出错误 300(“多项选择”)

【问题讨论】:

标签: url mod-rewrite url-rewriting


【解决方案1】:

你让自己很难受。如果您在 php.ini 中进行基本路由会更容易。只需向您的index.php 发送任何不是目录,也不是文件或php 文件的内容:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} (?>.*)(?<=\.php) [NC]
RewriteRule ^(?!index\.php$). index.php [NS,L]

index.php 的顶部,执行以下操作:

<?php

$url = explode('?', $_SERVER['REQUEST_URI'], 2);
$url = substr($url[0], 1);
if ($url) {
    $url = strtolower($url) . '.php';
    if (preg_match('@^[^./][^/]*(?:/[^./][^/]*)*$@', $url) && file_exists($url)) {
    .   # does not contain dotfiles, nor `..` directory traversal, so is a php file below web root
        include $url;
    }
    else {
        # virtual URL doesn't exist,
        # set 404 response code header and serve a default page
        include '404.php';
    }
    exit;
}

# no virtual URL, continue processing index.php

在每个页面的 &lt;head&gt; 中添加一个 rel=canonical,其中包含小写 URL(重新添加任何查询字符串),这样您就不会因重复内容而受到惩罚,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多