簡體   English   中英

.htaccess-重定向不起作用

[英].htaccess - Redirects are not working

public_html profile.php中有一個安全頁面,用於檢查用戶是否已登錄。未登錄時,它會重定向到login.php。當我嘗試訪問http://my-domain.com/profile時,它會起作用http://my-domain.com/profile/ (末尾有斜杠)

這是我的.htaccess

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</ifModule>

的index.php

<?php

    $path = $_SERVER['REQUEST_URI'];

    $chunks = explode("/", $path);

    //print_r($chunks);


    if($chunks[1] == "profile") {

        if($chunks[2] != "") {
            $profile_id = $chunks[2];
            require_once("profile.php");        
        } else {
            require_once("profile.php");        
        }

    }
?>

profile.php

<?php

    require_once("../includes/initialize.php");

if(!$session->is_logged_in()) {
        redirect_to("login.php");
    }

    if(isset($_GET['id'])) {
        $profile_id = $_GET['id'];
    }

    echo "This is the profile page of ID: ".$profile_id;

?>

那是因為帶有斜杠的是目錄,並且它與-d測試匹配。

我用這個來確保我永遠不會有斜杠

## Redirect to remove trailing slashes on extensionless URL requests 
# If requested URL ending with slash does not resolve to an existing directory 
RewriteCond %{REQUEST_FILENAME} !-d 
# Externally redirect to remove trailing slash 
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM