如何重定向www到apache服务器上cpanel中的非www next.js应用程序

wf82jlnq  于 2023-06-29  发布在  Apache
关注(0)|答案(1)|浏览(113)

我已经将我的next.js应用程序部署到cpanel作为一个独立的构建,当请求我的域名而没有www时,一切都正常工作,如下所示:example.com但是当我像www.example.com这样请求时,GET请求无法完成。
下面是.htaccess文件:

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]

如何使用301状态代码将http://www.example.com重定向到https://example.com
Apache版本:2.4.57

e3bfsja2

e3bfsja21#

要将所有请求重定向到非www,请在网站的.htaccess文件的开头添加以下行:

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

因此,编辑您的.htaccess文件的版本:

# 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]

您可以在评论中关注this question以获得更详细的信息。

相关问题