Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Armghan's avatar

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>

0 likes
4 replies
Snapey's avatar

the .htaccess inside the public folder should be the only one being used. Unless you have foolishly published the entire project?

1 like
Armghan's avatar

@Snapey Appreciate the reply.

I'd moved everything inside the laravel app folder to the hosting's public_html folder (where the domain is linked) and simply renamed the file htaccess to .htaccess. (Saw it on some stack forum).

Could you please direct me in the right direction and what I've done wrong?

I'm just a single lay who played around coding, learned and made something... I'm somewhat unfamiliar with how it's supposed to be done.

Armghan's avatar

@Snapey Okay, I just saw a tut on YT. I should've only uploaded the contents from the public folder to the public_html and kept the rest to a separate folder on the hosting. Then I need to edit with index.php to find the files from that separate folder.

I'll fix it and retry the cpanel redirect function and maybe it works this time.

Will update soon.

Armghan's avatar

@Snapey Alright, that "published the entire project" was the single line that helped me to see I had uploaded the website in an incorrect way. Fixed it! The contents of public folder from Laravel are the only contents inside cPanels public_html folder now. The .htaccess file is working perfectly with the redirect feature from cPanel.

Thanks again!!!

Please or to participate in this conversation.