php - 重定向跳过 www 是常见的做法吗?

标签 php .htaccess laravel http web

我想将我的网站宣传为 mydomain.com。对我来说,它看起来比 www.mydomain.com 好。用重定向修剪这个 www. 部分是一种常见的做法吗?有人会去 www.mydomain.com/books,我会把他重定向到 mydomain.com/books。这个想法背后有什么潜在的陷阱吗?

另一个原因是使第三方回调 URL 变得简单。现在,我需要将 mydomain.com/oauth/googlewww.mydomain.com/oauth/google 声明为我的重定向 URL,因为 Google 认为它们不同。这不是什么大问题,但需要额外考虑。

如果修剪 www. 是安全的,如何实现?我在 Apache 上使用 PHP。我当前的 .htaccess 看起来像这样:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

最佳答案

您应该使用 www 吗?

您应该在 www.example.com 或 example.com 上进行规范化,您的选择主要取决于您的偏好。关于哪个是正确的或应该使用哪个,存在不同的意见。

no-www.org提倡使用 www 子域是多余的,因为使用 http 协议(protocol)意味着网络。

By default, all popular Web browsers assume the HTTP protocol. In doing so, the software prepends the 'http://' onto the requested URL and automatically connect to the HTTP server on port 80. Why then do many servers require their websites to communicate through the www subdomain? Mail servers do not require you to send emails to recipient@mail.domain.com. Likewise, web servers should allow access to their pages though the main domain unless a particular subdomain is required.

yes-www.org另一方面提倡使用 www 子域,因为它可以让您的网站更加灵活。通过使用 www 子域,您可以通过在 dns 记录中使用 CNAME 来更轻松地使用诸如 CDN/云托管之类的东西。

When using a provider such as Heroku or Akamai to host your web site, the provider wants to be able to update DNS records in case it needs to redirect traffic from a failing server to a healthy server. This is set up using DNS CNAME records, and the naked domain cannot have a CNAME record.

当您在另一个域上托管静态 Assets 时,它还可以帮助防止不必要的 cookie 流量。

One common web site optimization is to serve static content from a subdomain, such as static.example.com. If you are using www, then this is no problem; your site’s cookies won’t be sent to the static subdomain (unless you explicitly set them up to do so).

一旦你决定了

您需要将所有流量重定向到您最终使用的版本。对于 apache,这可以使用 mod_rewrite 完成,如其他答案所示:

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

如果您能够编辑主要的 apache 配置,另一种选择是设置两个虚拟主机,其中一个只是重定向到另一个。我为我的网站这样做,效果很好。

<VirtualHost *:80>
    ServerName example.com

    Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www
    #The rest of your configuration
</VirtualHost>

关于php - 重定向跳过 www 是常见的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729554/

相关文章:

php - 如何统计数据库中meta_value的使用次数

php - 在提交 MySQL 之前将字符串转换为日期

php - 在 laravel 查询生成器中使用数字,其中语句返回正确的响应,但在使用包含数字的变量时返回错误的响应

php - Laravel API 如何获取图片的完整路径?

php - 如何通过 laravel 迁移将日期间隔存储到 MySQL 中?

php - 当使用 WHERE NOT IN 时,它返回 WHERE IN 之一

PHP "Warning: usort() [function.usort]: Invalid comparison function"排序

linux - 将 .htaccess 递归到子目录中?

apache - .htaccess 在文件不存在时重定向

php - 如果用户在页面名称后输入尾随 "/"(或 "/"之后的任何内容),我如何将用户重定向到错误 404 页面?