【问题标题】:Is there a way to automatically add a trailing slash to all URLs except for URLs ending with a file extension (i.e. .pdf, .html, ...)?除了以文件扩展名结尾的 URL(即 .pdf、.html、...)之外,有没有办法自动向所有 URL 添加尾部斜杠?
【发布时间】:2021-09-12 12:01:31
【问题描述】:

这是我的 .htaccess 文件:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^mywebsite\.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

这是我的 index.php 文件:

<?php

$GLOBALS["DEBUG"] = true;

$GLOBALS["DEBUG_MODE"] = "file";
// $GLOBALS["DEBUG_MODE"] = "stdout";
// $GLOBALS["DEBUG_MODE"] = "global";

$GLOBALS["DEBUG_REGEX"] = "^[^b]";
$GLOBALS["ERROR_LOG"]   = true;
// $GLOBALS["DEBUG_SQL"] = true;
// $GLOBALS["PROFILER_SQL"] = true;

// $GLOBALS["DEBUG_CLASS_WALK"]  = true;
// $GLOBALS["DEBUG_CLASS_FOUND"] = true;
// $GLOBALS["DEBUG_CLASS_MATCH"] = "c_";

define("DIR_WEBROOT", realpath(dirname(__FILE__)));

include DIR_WEBROOT . "/xfw/app/startup.php";

d();





// pr(db0()->oQueryFetchArray("SELECT * FROM core_contact"));


pr(r::parseUrl());



if ($a = x_Route::path("^/JSON/(JSON|RAW|DIRECT)/([^/]*)/([^/]*)")) {
    $class  = $a[2];
    $method = $a[3];
    
    $a_params = json_decode(trim(file_get_contents("php://input")), true);
    // $a_params = array_map('sql_escape_string', $a_params);

    try {
        $data = [
            "message" => "ok",
            "data"    => call_user_func_array("$class::$method", [$a_params]),
        ];
    } catch (Exception $e) {
        $data = [
            "message" => "Erreur",
            "data"    => $e->getMessage() . "\n\n" . getExceptionTraceAsString($e),
        ];

        h("err");

        pr(getExceptionTraceAsString($e));
        h("$class::$method");
        pr($a_params);
    }
    
    if ($a[1] == "JSON") {
        echo json_encode($data);
    } elseif ($a[1] == "DIRECT") {
        echo json_encode($data["data"]);
    } else {
        echo $data;
    }

    exit;
}


if ($a = r::path("^/$")) {
    header("Location: /fr/ ");

    exit;
}


if (!r::path("/$")) {
    header("Location: ".ROUTE_PATH."/");

    exit;
}


if ($a = r::path("^/fr/$")) {
    echo View::renderTemplatePublic("public/fr/home.html");

    exit;
}


if ($a = r::path("^/fr/contact/$")) {
    echo View::renderTemplatePublic("public/fr/contact.html", [
        "message" => c_Page::message()
    ]);

    exit;
}


if ($a = r::path("^/fr/(.*)/")) {
    echo View::renderTemplatePublic("public/fr/$a[1].html");

    exit;
}

我想要一个规则,该规则将在所有 URL 中添加一个尾部斜杠,但带有扩展名(.pdf、.html、...)的 URL 除外。谁能帮我实现这一点,这样当我有一个像 mywebsite.com/example.pdf 这样的 URL 时,它不会将我重定向到 mywebsite.com/example.pdf/ 并在末尾带有斜杠?

【问题讨论】:

  • 您显示的代码均未添加斜杠。第一个块是 HTTPS 重定向,第二个块是不存在的文件或文件夹的内部重写,到 index.php
  • 那我该如何解决呢?要添加什么?
  • “要添加什么?” - 首先是正确的问题描述。我们不知道您所说的那些尾随斜杠应该来自哪里,我们目前只能说,它们似乎来自重写配置到目前为止,您已经向我们展示了。
  • 我找到了重写 URL 的文件,以在末尾添加一个斜杠(对不起,我每天都在学习)
  • 那么这里可能需要修改if (!r::path("/$")) { header("Location: ".ROUTE_PATH."/");部分。我不知道r::path 是什么(到目前为止你没有提到你在这里使用的是什么系统/框架),但它似乎适用于正则表达式。 /$ 只检查值是否以斜杠结尾,并且条件是否定,如果不是,则重定向。

标签: .htaccess pdf url url-rewriting trailing-slash


【解决方案1】:

看起来像

if (!r::path("/$")) {
    header("Location: ".ROUTE_PATH."/");

    exit;
}

是这里需要修改的部分。这似乎是在使用正则表达式; /$ 只检查值是否以斜杠结尾,并且条件是否定,如果不是,则重定向。

与其尝试提​​出更复杂的正则表达式,我可能会在这里使用pathinfo。尝试将该部分替换为

if (!r::path("/$") && pathinfo(ROUTE_PATH, PATHINFO_EXTENSION) === '') {
  header("Location: ".ROUTE_PATH."/");

  exit;
}

PATHINFO_EXTENSION 标志使pathinfo 只返回扩展名,如果路径实际上没有任何扩展名,它只会返回一个空字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2011-12-11
    • 2021-12-14
    相关资源
    最近更新 更多