只需使用.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
,但您包含正確的文件。
只是爲了澄清,你想知道如何獲得'/ special/index.php'的請求來實際加載'/ special/laravel/public/index.php'還是完全關閉? – Jakar
是的,這正是我想要的,因爲已有的重定向規則。 – kdobrev
好的。我個人處理它的方式是我總是把我的'.htaccess'內部重定向到'/ dir/load_page.php',然後我的'load_page.php'處理其他所有內容。如果你想這樣做,我可以給你我的'.htaccess',然後其他的東西是有點簡單的PHP。否則,我可能會寫出你需要的規則,只需用'.htaccess'在內部重寫URL,但是後者的強健性較差。 – Jakar