【问题标题】:.htaccess code broke site?.htaccess 代码破坏了网站?
【发布时间】:2017-11-07 20:08:43
【问题描述】:

我尝试使用以下代码将新的 .htaccess 文件添加到 Drupal 站点的根目录:

# Redirect all users to access the site WITH the 'www.' prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但是现在我在不是主页的每个页面上都收到此错误。请帮忙!所以如果我去 www.website.com/products 我会得到这个:

Not Found
The requested URL /products was not found on this server.

Apache/2.4.7 (Ubuntu) Server at www.website.com Port 443

这个“未找到”是否与上面的 .htaccess 代码有关?

【问题讨论】:

  • 我认为这不太可能是原因。该 sn-p 仅用于在域名中强制使用www.。它没有做更多的事情,除了它不会尝试添加 www. 前缀,如果请求以看起来像 file.extension 的东西结尾。
  • 感谢您的回复!我认为在添加 .htaccess 的同时注意到错误可能只是巧合,但我不确定。如果 mod_rewrite 没有打开,如果添加 .htaccess 会搞砸并且 Apache 需要重启?
  • 是的,如果您在未启用 mod_rewrite 的情况下使用此功能,然后启用,则需要重新启动该更改才能生效;即,加载新模块。也就是说,在正常情况下,您不需要在编辑 .htaccess 文件后重新启动 Apache,不需要。
  • “添加新的 .htaccess 文件”是指您将此代码添加到现有(Drupal 的).htaccess 文件中,还是用您的替换旧文件?
  • @MilanG 实际上我不小心覆盖了 Drupal .htaccess 文件,因为它是隐藏文件

标签: php apache .htaccess drupal


【解决方案1】:

端口 443 通常用于 HTTPS。处理这个问题的最好方法是使用虚拟主机。使用 vhost 路由,您可以尝试以下操作:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect "/" "https://www.example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost>

走 .htaccess 路线,你可能想尝试这样的事情:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com*
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

【讨论】:

    【解决方案2】:

    好的,所以我发现这里的问题是我上传了一个 .htaccess 文件,但没有看到已经存在的文件,因为该文件隐藏 在服务器上,所以我实际上不小心覆盖了错误的 Drupal .htaccess 文件。一旦我们恢复到原始的.htaccess 文件并将此代码添加到其中,一切正常。

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多