2016-04-22 95 views
0

我怎樣才能做到以下可能.htaccess?限制子目錄登錄用戶

/sp/sitea => /check.php?page=sitea #no trailing forward slash 
/sp/sitea/ => /check.php?page=sitea #trailing forward slash 
/sp/sitea/index.php => /check.php?page=sitea/index.php #includes a file 
/sp/siteb => /check.php?page=siteb 

/sp/index.php , /sp/login.php => no redirect 

然後我想用PHP的站點A,看看用戶是否登錄,然後重定向到/ SP /站點A如果他們還沒有登錄該站點重定向到/login.php

檢查DB

我試過的.htaccess下面,在SP文件夾,但犯規重定向,我不是真正的.htaccess主

RewriteEngine On 
RewriteCond %{REQUEST_URI}/
RewriteRule ^\/(.*)$ sp/check.php?path=$1 [L] 
+0

你是什麼意思 「但不工作」? –

+0

@MatthewCliatt它不重定向 – tsukimi

+0

你想在你的文章中包含這些信息。 –

回答

0

我解決了這個與修改.htaccess文件

RewriteEngine on 
RewriteOptions MaxRedirects=2 
RewriteBase/

#dont check non pages 
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$ 
RewriteRule ^(.*)/(.*)$ /sp/check.php?path=$1&path2=$2 [L,P] #P keeps address in bar after redirect 

check.php

<?php 
//htaccess redirects here for sitepreview subdomains, then do authentication check and output the html 
//cant redirect to page or get redirect loop 
session_start(); 
$dir = $_GET["path"]; 
$dir2 = $_GET["path2"]; 
//echo $dir; 

if(!isset($_SESSION['login_user'])){ 
    header("Location: /sp/index.php",true,301); 
    exit(); 
}else { 
    $name = $_SESSION['login_user']["name"]; 
    if($name == $dir) { 
     $homepage = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/sp/$dir/" . (isset($dir2) && strpos($dir2, ".htm") ? $dir2 : "index.html")); 
     echo $homepage; 
    } 
    else { 
     header("Location: /sp/index.php",true,301); 
     exit(); 
    } 
    exit(); 
} 
+1

你可以擴展你如何完全修復它嗎?這可能會在未來幫助其他人。 – Bono

+1

@bono更新.... – tsukimi