重定向到文件夹后重写URL为漂亮 - htaccess laravel

时间:2014-07-02 18:22:38

标签: .htaccess mod-rewrite

您好我的服务器上有一个“protected”文件夹。在用于某些用户的条件重定向的.htaccess文件中,我使用以下规则:

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

在/ special文件夹中,我有一个.htaccess文件,其中包含以下规则:

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

该文件夹中的应用程序将基于laravel,因此我的内容必须通过驻留在/special/laravel/public/index.php

中的index.php文件提供

我希望网址看起来像/ special /.

我应该制定什么规则以及在何处发生这种情况?

这是对我上一个问题的跟进:

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

1 个答案:

答案 0 :(得分:0)

只需使用.htaccess重写网址:(进入/

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,则会导致服务器在内部重定向到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并且他们没有看到您将网址更改为special/laravel/public,但是您包含了正确的文件。

相关问题