the .htaccess inside the public folder should be the only one being used. Unless you have foolishly published the entire project?
Redirect "Non-www" domain to "www" (or inverse) from within code or Hosting/Cpanel htaccess. Which is the best practice?
I am trying to make small corrections to my Laravel website for future SEO purposes. I'm redirecting as such to remove Search Engines indexing both www and non-www links.
The Redirect option from the cPanel itself doesn't seem to work. (Most likely due to some conflict with Laravel). Since the basic htaccess file from the main laravel folder has some code that already redirects to the public folder and index.php file. So when I do it from cpanel, it places a couple lines in the htaccess but obv it doesn't work. Now alternatively, I just made a simple middleware for all routes inside the app and it helps in checking and redirecting (Very Simple and Easy).
There is a slight delay in the redirection (Now that could be because of my code, which I'm also fixing). Maybe I could remove middleware and add that code to the AppServiceProvider, so it happens in the very initial request?
- Note: Ignore the rest from below if the current solution is an alright practice.
Now I'm not familiar with the expressions needed for the htaccess file and most of posts I find online seem to be different from the code cPanel currently writes in the htaccess. it is my own assumption that this redirect doesn't work probably because of the what is already written inside for laravel Here's the default redirection needed inside laravel's Main htaccess itself:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/ [L,NC]
</IfModule>
Another htaccess file found inside the public folder of the app:
<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>
Please or to participate in this conversation.