.htaccess redirect from subfolder to domain not working properly

Viewed 684

I am using PHP, Laravel 8 Framework, and cPanel for my website. What I am trying to do is something like this.

Redirecting Routes from

http://example.com/example.com/public/
http://example.com/example.com/public/about
http://example.com/example.com/public/contact
.
.
// more

to

https://example.com/
https://example.com/about
https://example.com/contact
.
.
// more

I have the .htaccess file in the domain root public_html folder.

.htaccess file

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /example.com/public/$1 [L]

Please guide me if I had done something wrong.

1 Answers

Please try following rules, with your shown attempts. Please make sure to clear your browser cache before testing your URLs. Make sure 2 things. 1st- Keep your htaccess rules file in same level where you have folder example.com and 2nd- keep your index.php inside /root/example.com/public folder.

Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/example\.com/public [NC]
RewriteRule ^(.*)/?$ example.com/public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ example.com/public/index.php [L]
Related