简体   繁体   English

.htaccess-重定向不起作用

[英].htaccess - Redirects are not working

There's a secure page in public_html profile.php which checks whether the user is logged in. It redirects to login.php when not logged in. It works when I try to access http://my-domain.com/profile but not when http://my-domain.com/profile/ (slash at the end) public_html profile.php中有一个安全页面,用于检查用户是否已登录。未登录时,它会重定向到login.php。当我尝试访问http://my-domain.com/profile时,它会起作用http://my-domain.com/profile/ (末尾有斜杠)

here my .htaccess 这是我的.htaccess

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

index.php 的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 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;

?>

Thats because with the trailing slash it is a directory and it matches the -d test. 那是因为带有斜杠的是目录,并且它与-d测试匹配。

I use this bit to make sure I never have a trailing slash 我用这个来确保我永远不会有斜杠

## 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