I have a website built on Laravel 9. Now for some reason, I need to add a folder/directory in the root (httpdocs) named "news".
When I go to my website e.g. mywebsite.com, Laravel website works fines as expected but when I go to mywebsite.com/news, the Laravel mechanism kicks in and shows an error page.
So my question is How can I make it (either through .htaccess or etc) so that Laravel does not get activated when I access the directory "news" and the contents inside "news"?
My .htaccess in root currently have this three lines
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
And .htaccess inside the public folder contains this –
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I am on the Apache Plesk server. There are similar looking questions on StackOverflow but none mets my requirement
2
Answers
Well, you implement an exception rule before the rules used by Laravel. Your rule in the "root" folder (I assume you refer to the folder configured as
DOCUMENT_ROOT
in your http server’s host configuration) rewrites all incoming requests to the "public" folder. The rules implemented in there probably are maintained by Laravel? Again the last rule rewrites all requests to the controller.So you want to implement an exception that prevents requests to
/news/...
to get rewritten to Laraval, right? Just add such a rule prior (so above) the existing rule currently rewriting everything to Laraval:That additional rule will not rewrite requests to resources starting with a path
/path/
. Requests to/path
(without trailing slash) will still get rewritten to/public/news
.Note that I also fixed the
RewriteCond
. You need to implement a pattern testing against the absolut requested path. That is explained in the documentation: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecondIt is a good idea to test your implementation using a test tool, especially when you are still trying around. Have a try here:
https://htaccess.madewithlove.com?share=70cc8414-53ed-48cc-9dc8-758e0376c775
I personally would prefer to implement such general rules in the central http server’s host configuration, not in a distributed configuration file (".htaccess") as you currently do. Various reasons for that. But maybe you do not have access to the actual, central configuration, for example if you are using a cheap hosting provider.
You can add another condition (
RewriteCond
directive) to the root.htaccess
file to prevent the request being rewritten to the (Laravel)/public
subdirectory. However, your existing condition is incorrect and will always be successful (although strictly speaking this is not necessary anyway – which is why it still "works" – since the presence of the/public/.htaccess
file effectively prevents the rewrite loop). It should read^/public
(with a slash), not!^public
.For example:
The first condition is successful only when the
REQUEST_URI
variable (root relative URL-path) does not (!
) start with/news
.The two conditions can be combined if you wish by changing the regex. ie.
!^/(public|news)
.Alternatively, add the
news
directory in thepublic
subdirectory (ie./public/news
) then you don’t have to change either.htaccess
file. (Although you should still "correct" the root.htaccess
file as mentioned above.)