[英]how to write .htaccess and protect sites files and folder in laravel
我在laravel開發了一個網站,但問題是,如果有人知道文件名,他可以直接訪問我不想要的網站文件
JobScholar
app
Exceptions
Http
Mail
Models
Permission.php
Providers -
Role.php
User.php
.env
etc etc
應用程序運行正常,但是如果用戶輸入我想要什么
http://localhost:8080/JobScholar/app/
or
http://localhost:8080/JobScholar/User.php/
他應該重新分配到家庭控制器或看到錯誤消息
我的.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Options -Indexes
ErrorDocument 403 http://localhost:8080/JobScholar/index
Options +FollowSymLinks
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /JobScholar/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
## Don't listing directory
#Options -Indexes
#
## Follow symbolic links
#
#
## Default handler
#DirectoryIndex index.php
我不希望用戶訪問任何文件或directory
,他可以看到的唯一目錄和文件只是那些routes
已定義的文件
Laravel基本流程是,所有的請求都通過index.php
文件中的public
文件夾。 因此,您必須確保服務器將所有請求都指向該文件夾,以便index.php
可以服務並處理其余的請求。
快速解決方案是創建vHost文件(假設您不在共享主機上)。 這是一個示例:
<VirtualHost *:80>
ServerName www.site.com
ServerAlias site.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/site_name/public
<Directory /var/www/html/site_name/public>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
在apache服務器上,它將進入:
/etc/apache2/sites-available/
然后啟用站點a2ensite site_name.conf
最后,將站點指向/etc/hosts
服務器IP,如下所示: 127.0.0.45 site.com
此外,一定要使用能夠國防部重寫a2enmod rewrite
這樣.htaccess
通過Laravel提供可以使需要重新寫入的網址。
最終重新啟動apache: service apache2 restart
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.