Redirect all www URL to non www and make laravel-project/public 404 not found

20 Views Asked by At

I want to achieve these 3

  1. Non www urls for the site

  2. www url redirect to their respective non www url

  3. example.com/public - should be 404 page not found

I deployed my laravel project on server's root directory, it is on shared hosting, I can't change the document root so i put a htaccess file to redirect all request to /public and my site starts working

but now the problem is these are accessible

example.com/ and any-other-url

www.example.com/ and any-other-url

example.com/public and any-other-url

www.example.com/public and any-other-url

To achieve the 3rd step I moved my project under home directory (i.e ../public_html) and laravel's public folder files to public_html, made the required changes in index.php and site starts working fine

Now, example.com/public is showing 404 page not found. - step 3 achieved

Also as I have laravel default htaccess file under public_html folder I made 2 changes to achieve the step 2


<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 %{HTTP_HOST} ^www\.(.*)$ [NC]<-added this
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteRule ^ index.php [L]<- commented
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]<- added this
</IfModule>

This results in

example.com - accessible

www.example.com - accessible but not redirecting to non www

www.example.com/any-url properly redirecting to example.com/any-url but results in 404 page not found

I am stuck here. Any advice please

0

There are 0 best solutions below