我在共享托管服务器中使用Laravel 5.2。我想从url中删除public而不将根目录中的server.php更改为index.php,因为.env和其他文件是公开可见的。我的.htaccess如下所示:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,NC]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]
</IfModule>
您需要将域指向cpanel中的公用文件夹。
或
将.htaccess文件从/ public目录移动到根目录,并将其内容替换为以下代码:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
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>
创建新根目录中的.htaccess文件并粘贴以下代码
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php56___lsphp .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
最佳解决方案是将您的apache文档根指向laravel的公用文件夹。但是正如您所说的,您正在使用共享主机,因此might对您来说不可能。
现在无需在网址中公开即可访问laravel应用,请遵循此问题Laravel 5 - Remove public from URL
此外,您担心其他文件夹(例如应用程序,配置等)无法从浏览器访问。因此,您可以将.htaccess
您要阻止从浏览器访问的每个文件夹中的内容为Deny from all
的文件。
在根文件夹中,用。制成.htaccess文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
这是我找到的确切的工作解决方案。解决方案取自here,由Pallav Nagar回答
在根目录中创建.htaccess文件,并按如下所示放置代码。
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
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>