Sheese Sheikh
Sheese Sheikh

Reputation: 1

How to remove trailing subfolder/public/index.php from the Laravel 9 website URL on shared hosting?

I'm using Godaddy's shared hosting and the domain I'm using is the primary domain of the cPanel account, hence, I cannot change the document root for the Laravel site. With the current .htaccess codes deployed, the homepage website URL shows up as - https://www.example.com/subfolder/public/index.php instead of https://www.example.com How do I remove the trailing /subfolder/public/index.php from the website?

I'm using the following .htaccess code in public_html to redirect traffic to the subfolder:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subfolder/public/ 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ subfolder/public/$1 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ subfolder/public/index.php [L] 
</IfModule>

And, this is the .htaccess code (also contains force HTTPS and www) inside subfolder/public:

RewriteCond !{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ 
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

I've tried multiple variations of .htaccess found across multiple websites, but it hasn't resolved the issue.

Upvotes: 0

Views: 596

Answers (1)

Sheese Sheikh
Sheese Sheikh

Reputation: 1

Folder structure >> home/user/public_html/subfolder/public/index.php

Got the trailing issue resolved by updating the public_html .htaccess:-

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond !{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ 
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_URI} !^/subfolder/public/ 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ subfolder/public/$1 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ subfolder/public/index.php [L] 
</IfModule>

The .htaccess inside the public folder is the default Laravel code:-

<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've removed the .htaccess inside the subfolder.

Upvotes: 0

Related Questions