重定向到站点并跳转到锚点

时间:2019-10-25 06:46:13

标签: javascript .htaccess

我尝试重定向到站点并跳转到该站点的锚点。

锚点为#tab-5

我尝试过:

window.location.href = "/property/details?id=123#tab-5";

window.location.replace("/property/details?id=123#tab-5");

我还尝试了带有域名的完整URL。

但是该站点不会重新加载。该URL仅被替换,并且不会跳转到锚点。


.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

index.php

<?php
ob_start();
session_name("xxx");
session_start();

include("app/Routes.php");
$template = checkRoute();

// Redirect to login page if user is not logged in
if (!$template && !isset($_SESSION["logged_in"])) {

    $bodyClass = "login";
    $template = "templates/php/login.php";

// Redirect to start page (list view) if user is logged in and no url is given
} else if (!$template) {

    header("Location: /property/list");
    exit;
}

// Redirect to template
if (file_exists($template)) {
    require_once($_SERVER["DOCUMENT_ROOT"] . "/master.php");
    exit;
}

app / Routes.php

<?php
/**
 * A .htaccess rule always calls the index.php if a file could not be found.
 * This method gets called by index.php and creates a path out of the request
 * url path and returns the affiliated template file if exist. If nothing is found,
 * then return "no-route" template.
 */
function checkRoute()
{
    $redirectUrl = (isset($_SERVER["REDIRECT_URL"])) ? $_SERVER["REDIRECT_URL"] : "";
    $_SERVER["REDIRECT_URL"] = "";

    if (!empty($redirectUrl) && $redirectUrl !== "/login") {

        $templateRoot = "templates";
        $template = $templateRoot . "/php". $redirectUrl .".php";

        if ( ! file_exists($template)) {

            $template = "/". $template;

            if ( ! file_exists($template)) {
                $template = $templateRoot . "/html/no-route.html";
            }
        }

        if (file_exists($template)) {
            return $template;
        }
    }

    if (!empty($_SESSION["logged_in"])) {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我通过在紧接其后调用window.location.reload()来解决了该问题