2014-07-02 112 views
0

你好,我的服務器上有一個「受保護的」文件夾。在對一些用戶來說我用下面的規則的條件重定向它的.htaccess文件:重定向到文件夾後重寫url爲漂亮 - htaccess laravel

RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4* 
RewriteCond %{REQUEST_URI} !^/special 
RewriteRule ^$ /special [R,NE,NC] 

在/特殊文件夾我​​有以下規則.htaccess文件:

RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$ 
RewriteRule ^(.*)$/[R=301,NE,NC,L] 

中的應用該文件夾將基於laravel,所以我的內容將不得不從index.php文件中提供,其位於/special/laravel/public/index.php

我希望URL看起來像/ special /。

我應該放什麼規則以及在哪裏發生?

這是一個跟進我剛纔的問題:

https://stackoverflow.com/questions/24487012/redirecting-specific-ip-to-special-content-htaccess-vs-php

+0

只是爲了澄清,你想知道如何獲得'/ special/index.php'的請求來實際加載'/ special/laravel/public/index.php'還是完全關閉? – Jakar

+0

是的,這正是我想要的,因爲已有的重定向規則。 – kdobrev

+0

好的。我個人處理它的方式是我總是把我的'.htaccess'內部重定向到'/ dir/load_page.php',然後我的'load_page.php'處理其他所有內容。如果你想這樣做,我可以給你我的'.htaccess',然後其他的東西是有點簡單的PHP。否則,我可能會寫出你需要的規則,只需用'.htaccess'在內部重寫URL,但是後者的強健性較差。 – Jakar

回答

0

只需使用.htaccess重寫URL:(進去/

DirectoryIndex index.php 

#Redirect to /special/laravel/public if you haven't already and the IP is okay 
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4* 
RewriteCond %{REQUEST_URI} !^/special/laravel/public 
RewriteRule ^(/special)(/laravel)?(.+) /special/laravel/public$2 [L] 

#if IP does not match and you ARE in the folder, then Redirect to root 
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4* 
RewriteCond %{REQUEST_URI} ^/special/laravel/public 
RewriteRule .?/[R=301,L] 

我認爲這會工作。我沒有測試它。如果你需要我,我可以加入它。當然對於您的用例,您可能需要在其中添加更多RewriteCond以驗證REMOTE_ADDR

我處理它的方式:

的.htaccess:

RewriteCond %{REQUEST_URI} !^/load_page.php$ 
RewriteRule ^(.+)$ /load_page.php [QSA, L] 

這導致服務器內部重定向到load_page.php如果請求的URL是不是load_page。沒有RewriteCond,我相信它會導致無限的重定向。這應該可以工作,但我沒有測試它,因爲它與我的實現不同,因爲我也處理重寫URLS具有尾隨斜槓並且從不顯示.php,這使得它更復雜一些,截然不同。

load_page.php

$SITE_ROOT = $_SERVER['DOCUMENT_ROOT']; 
$URL = $_SERVER['REQUEST_URI']; 

ob_start(); 
if (conditionOne){ 
    //change $URL here if you need to or do nothing 
} else if (conditionTwo){ //check the IP or whatever you want to here 
    if (aCondition){//if $URL starts with '/special' 
     $URL = str_ireplace('/special/','/special/laravel/public/',$URL,1); 
    } 
    if (is_dir($SITE_ROOT.$URL))$URL .='index.php'; 
} 
if (!file_exists($SITE_ROOT.$URL))$URL = '/error/404.php'; 
include($SITE_ROOT.$URL); 
$content = ob_get_clean(); 
echo $content; 

它的東西影響。用戶看不到load_page.php,他們沒有看到您將URL更改爲special/laravel/public,但您包含正確的文件。

+0

謝謝你的回答,我會(在後期)肯定實現PHP版本。但現在我用.htaccess文件將它全部扭曲了。所以我感到困惑的是:我有3個物理文件夾/,/ special和/ special/laravel/public。我應該放哪個.htacces文件的表述? – kdobrev

+0

我在想,我需要在Apache配置中使用別名。 – kdobrev

+0

我編輯了我的答案。我可能只會使用一個'.htaccess',因爲我個人覺得更容易管理。如果你需要更多的東西,並且以「if(thisConditionIsTrue){做這個} else if(thisOtherConditionIsTrue){做那個}」這樣的相當簡單的方式告訴我你想要什麼,那麼我可以嘗試寫出正在運行的'.htaccess'。但是,根據我的經驗,htaccess的大部分困難都來自正則表達式的困難。如果這是你的問題,那麼請查看http://www.regexr.com/,否則你可能需要閱讀更多http://httpd.apache.org/docs/current/rewrite/intro.html – Jakar

相關問題