【问题标题】:Laravel Project on Ubuntu: Issue redirecting www url to non www urlUbuntu 上的 Laravel 项目:将 www url 重定向到非 www url 的问题
【发布时间】:2022-08-22 02:42:51
【问题描述】:

我有一个托管在 AWS EC2 Ubuntu (ngnix) 上的 Laravel 9 项目。该域已向 GoDaddy 注册,我更改了 A 记录以将其指向 AWS EC2 上的 IP。当我使用像 example.ca 这样的 url 时,我的网站运行良好,但是,当我使用 url 时它不起作用www.example.ca(我得到默认值欢迎使用 nginx!页)

根据谷歌搜索,我在 public/.htaccess 中添加了以下重写规则

RewriteCond %{HTTP_HOST} ^www.example.ca [NC]
RewriteRule ^(.*)$ http://example.ca/$1 [L,R=301]

但是,它没有帮助。我仍然继续获取默认的 Nginx 页面。

还有什么我需要做的吗?例如,在启用站点.我想补充一点,当我设置项目时,我在/etc/nginx/sites-available包含以下内容的文件夹

server {
    listen 80;
    listen [::]:80;
    server_name example.ca;
    root /var/www/vhosts/example.ca/public;

    add_header X-Frame-Options \"SAMEORIGIN\";
    add_header X-Content-Type-Options \"nosniff\";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \\.php$ {
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\\.(?!well-known).* {
        deny all;
    }
}

非常感谢任何帮助。

    标签: laravel .htaccess nginx


    【解决方案1】:

    改变server_name example.ca;server_name example.ca www.example.ca;解决了这个问题。 (感谢安德鲁圣丹尼斯。)

    server {
        listen 80;
        listen [::]:80;
        server_name example.ca www.example.ca;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-24
      • 2019-03-09
      • 2011-11-05
      • 2013-12-20
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      相关资源
      最近更新 更多