0

I have deployed my next.js app to cpanel as a standalone build and everything works properly when requesting my domain without www like this: example.com But when I request like www.example.com the GET request fails to complete.

Here is the .htaccess file:

RewriteEngine on
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/username/public_html/App.Next"
PassengerBaseURI "/"
PassengerNodejs "/home/username/nodevenv/public_html/App.Next/18/bin/node"
PassengerAppType node
PassengerStartupFile /home/username/public_html/App.Next/server.js
PassengerAppLogFile "/home/username/logs/passenger.log"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]

How do I redirect http://www.example.com to https://example.com with 301 status code?

Apache Version: 2.4.57

Hooman Limouee
  • 1,143
  • 2
  • 21
  • 43

1 Answers1

0

To redirect all requests to non-www, add the following lines at the beginning of your website’s .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

So edited version of your .htaccess file:

# Force www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteEngine on
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/username/public_html/App.Next"
PassengerBaseURI "/"
PassengerNodejs "/home/username/nodevenv/public_html/App.Next/18/bin/node"
PassengerAppType node
PassengerStartupFile /home/username/public_html/App.Next/server.js
PassengerAppLogFile "/home/username/logs/passenger.log"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]

You can follow this question for more detailed info at comments.

halil şen
  • 16
  • 2