PHP htaccess 错误与 URL 的


PHP htaccess wrong with URL's

http://localhost/customers/website/jobs/28

例如,当访问上面的 URL 并使用我的代码时,它会转到:http://localhost/customers/website/jobs/startpage

if(isset($_COOKIE["startpage"])) {
    }else{
        header("Location: startpage");
    }

但如果我这样做header("Location: /startpage");它就会变得 http://localhost/startpage

我的Htacess是:

RewriteEngine On
RewriteBase /customers/website/
RewriteRule ^startpage/?$    startpage.php    [NC,L]

我怎样才能让它去 http://localhost/customers/website/startpage?

不要为此使用 PHP。你可以在htaccess本身中执行此操作:

RewriteEngine On
RewriteBase /customers/website/
RewriteCond %{HTTP_COOKIE} !startpage
RewriteRule !^startpage/?$ startpage [L,NC,CO=startpage:1:%{HTTP_HOST},R=302]
RewriteCond %{HTTP_COOKIE} startpage
RewriteRule ^startpage/?$ startpage.php [L]

并从 PHP 代码中删除header函数调用。


如果你想在PHP本身中做到这一点,那么使用:

if(isset($_COOKIE["startpage"])) {
   //
} else {
   header("Location: " . $_SERVER["BASE"] . "startpage");
}

并保留有问题的相同重写规则。