Laravel .htaccess在访问url.com/public时重定向到主页

时间:2018-10-11 14:27:46

标签: laravel .htaccess

我的网站在以下网址 www.mywebsite.com 中运行没有任何问题。因此,在创建项目时,我在根文件夹中应用了一些.htaccess规则,如下所示:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Remove www    
    RewriteCond %{HTTP_HOST} ^www.mywebsite.com$ [NC]
    RewriteRule ^(.*)$ https://mywebsite.com$1 [R=301,L]

    # Remove http and force https   
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [QSA,R,L]

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</IfModule>

问题在于,如果我访问www.mywebsite.com/public,我仍会浏览该网站,但未应用CSS(因为URL错误)。

如何在不启用我的网址www.mywebsite.com/public的情况下将URL www.mywebsite.com重定向到www.mywebsite.com/public/img/someimage.png


编辑:要使自己清晰明了:

www.mywebsite.com -> it's working -> I don't want to change that

www.mywebsite.com/public/img/someimage.png -> it's working -> I don't want to change that

www.mywebsite.com/public -> It's working -> I see my website without the CSS applied -> I WANT to change this by redirecting users to www.mywebsite.com

2 个答案:

答案 0 :(得分:1)

您绝不允许访问public文件夹之外的任何内容。

您的服务器应设置为转到public,而不是其上方的主文件夹。请勿将index.php文件从public文件夹中移出。

答案 1 :(得分:0)

已解决

通过编辑文件/public_html/public/index.php,我可以检查当前URL是否符合我的条件。

在我的情况下,只要字符串public出现在URL中,它总是带有两个斜杠(因为.htaccess),例如:

www.mywebsite.com/public/

话虽如此,在文件顶部:

if ($_SERVER['REQUEST_URI'] == '/public/')
{
    header('location: http://' . $_SERVER['HTTP_HOST']);
    exit;
}