phplaravelapache.htaccesshttp

Remove public form URL in Laravel when uploaded on cPanel also always redirect to https://


so I have uploaded my project to cPanel's public_html folder and my URL is https://website.com but my URL looks likehttps://website.com/public whenever I try to get https://website.com some wired URL comes with a 404 or continues loading the page. so how can I use .htaccsess and make it so that my URL https://website.com/about look like this, not like this https://website.com/public/about

also, i want all my http:// request to go on https://

After finding a lot I found this line but it sometimes works and sometimes doesn't.

RewriteRule ^(.*)$ public/$1 [L]

here is my website's root folder's .htaccsess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php81” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php81 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

Also, make the answer general so that I or anyone can use it in any situation. and provide any explanation link because I want to learn this.


Solution

  • <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    

    explanation

    Redirect HTTP to HTTPS: If the request is not secure (HTTP), the first rule permanently redirects the user to the HTTPS version of the URL.

    Serve from public directory: For any URL request (after checking HTTPS), the second rule rewrites the URL to serve files from the public/ directory, which is typical in Laravel applications.