简体   繁体   中英

.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)

here my .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;

?>

Thats because with the trailing slash it is a directory and it matches the -d test.

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] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM