Protecting Laravel Root Directory - .htaccess

I'm having a problem with denying access to the root directory of my Laravel Site...
I have my project in xampp/htdocs/laravel
Of course Laravel routes the /public directory...
But how can I deny access to the directory xampp/htdocs/laravel ...?
How can I prevent the directory listing?
The only way I've found is putting an index.php with a header("location: /laravel/public")
But I dont know if it's the best way... should I use .htaccess? How?
I've tried a few but it results in major errors across the server.

You need to open your httpd.conf file (xampp/apache/conf/httpd.conf) and change the "DocumentRoot" value to include the path to your public folder:
DocumentRoot "C:/xampp/htdocs/laravel/public"

Or just add a .htaccess with :
RewriteEngine On
RewriteRule ^ public [L]

Related

htaccess rule to show my website under a specific path when accessing root domain URL

I have my website inside the following path on my remote web hosting server:
~/public_html/website/app
and need to find a way to show it when typing:
mydomain.com
so without the need of typing the full path:
mydomain.com/website/app/
One solution that came to my mind was to create a index.php file inside my root folder to automatically redirect to ./website/app/:
<?php
header('Location: ./website/app/');
exit;
but then I need to rewrite the URL in order to go from:
mydomain.com/website/app/
to:
mydomain.com
and I did not find a way to do that!
So, what do you think I should do to obtain that result and show my website under /website/app/ when accessing:
mydomain.com
and do not show the /website/app/ folder names?
Thanks a lot!
edit:
I was forgetting that I also have the website/server path so I need a rule that keeps valid the requests containing mydomain.com/website/server/.
Plus, I cannot use:
Options +FollowSymLinks
on my hosting (for security reasons), it's overridden by:
Options +SymLinksIfOwnerMatch
You have two options to do that.
If you just want to show the /website/app directory for your root url / then you can use DirectoryIndex directive .
DirectoryIndex website/app
This will show you the index file from /website/app folder if you visit the your root URI ie : example.com/ .
If you want to server files and directories from the /website/app instead of the / folder then you can use a RewriteRule that rewrites your root level requests to the subfolders .
RewriteEngine on
RewriteRule !website/app /website/app℅{REQUEST_URI} [L]

Symfony 3.4 located in subdirecory, remove web/ from url

I have 3 php apps on one server. I cant modify any apache files. Haw can I set .htaccess to remove web/ folder from url?
192.168.45.54/app1/
192.168.45.54/app2/
192.168.45.54/app3/web - I want to change it to 192.168.45.54/app3/
On adress 192.168.45.54/app3/web/ everythink is working fine. But haw can I remove web/ from url?
I created .htaccess files:
RewriteEngine On
RewriteBase /app3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
But when I go to 192.168.45.54/app3/ in symfony log files I have fallowing error:
No route found for "GET /app3/"
I use Symfony in 3.4 version. I think that problem is in .htaccess located in /web directory... Haw to configure .htaccess corectly?
I can't create virtual host becouse I haven't access to the server...
You need to make the web/ directory the document root of your website. If you have direct access to the webserver’s virtual host configuration, change the document root to the following line and restart Apache:
DocumentRoot /path/to/symfony/web/
If you can’t modify the virtual host file directly, your hosting provider will usually give you the possibility to modify the document root through some kind of online tool.

remove public from url in laravel

I have seen many questions like this, which solve in removing public in cases like example.com/public. But in my case i have example.com/cases/public. How to deal with this type of situatations.
Follow this steps to remove the public from url
Copy .htaccess file from /public directory to Laravel/project root folder.
Rename the server.php in the Laravel/project root folder to index.php.
Cheers your will be good now.
Please Note: It is tested for Laravel 4.2, Laravel 5.1, Laravel 5.2, Laravel 5.3.
I think this is the easiest way remove public.
If you want to remove cases/public from url than you can use this
RewriteBase /
RewriteRule ^cases/public http://example.com/ [R=301,L]
Remove direction R=301 in above line to make it internal redirection

Deny access to subdirectories using htaccess

Consider these files structures on web root directory :
files/1/1.jpg
files/1/2.jpg
files/2/1.jpg
files/2/3.jpg
files/3/6.jpg
files/3/8.jpg
files/4/1.jpg
I want to deny access to files inside folder 2 and 3 using htaccess file that exists in web root directory. I try but nothing happend. Here's the code I used:
<FilesMatch "(2|3)\/*$" >
Order deny,allow
Deny from all
</FilesMatch>
Would you correct my mistake?
Thank you.
If your htaccess file is in your web root, and the files directory is also in the web root, you won't be able to match against files in another (sub)directory. You can either try putting the <FilesMatch> in an htaccess file in the files directory, or you can use mod_rewrite in the htaccess file in your web root:
RewriteEngine On
RewriteRule ^files/(2|3)/ - [F,L]
For optimal performance using htaccess, you should create a .htaccess file in each of the directories you want to protect:
Order deny,allow
Deny from all

Disable access to a subfolder using .htaccess

I have a site in /public_html folder and I have another one in /public_html/addondomain.com.
I want to disable acesss to www.domain.com/addondomain.com but users should still be able to access www.addondomain.com (contained inside public_html/addondomain.com/ folder). I have an .htaccess in public_html folder. How do I do this?
Thanks in advance.
Try:
RewriteRule ^addondomain.com /this.file.does.not.exist

Resources