【问题标题】:.htaccess, forcing a slash and www.htaccess,强制使用斜线和 www
【发布时间】:2025-12-19 22:55:17
【问题描述】:

有人可以帮我强制我的网站重定向到使用 www。并且总是在任何页面的末尾添加一个斜杠?我的 htaccess 文件目前看起来像这样:

RewriteEngine on

RewriteRule blog/date/([^/]+)/?$ index.php?page=viewblog&date=$1
RewriteRule blog/([^/]+)/?$ index.php?page=viewblog&category=$1
RewriteRule blog/([^/]+)/([^/]+)/?$ index.php?page=viewblog&category=$1&title=$2

RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9]+)

提前非常感谢:)

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    这适用于任何域:

    DirectorySlash on
    RewriteCond %{HTTP_HOST} !^www [NC]
    RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L]
    

    这将在必要时添加斜线。 (%{REQUEST_URI} 将是 / 请求 root 或 /whatever 如果您请求 domain.com/whatever。)它也不会为与其他解决方案一样的主域。 DirectorySlash 指令确保在适当的地方添加斜线,即使 www 已经存在。

    【讨论】:

      【解决方案2】:

      我不知道如何添加斜杠,但是在 URL 前添加 www 很简单。 这是我用来在 url 之前添加 www 的方法:

      RewriteEngine on
      
      Options FollowSymlinks
      
      rewritecond %{http_host} ^yourdomain.com [nc]
      
      rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]
      

      用谷歌搜索结尾的斜线,发现这篇文章:http://www.mydigitallife.info/2007/03/19/add-trailing-slash-to-the-end-of-the-url-with-htaccess-rewrite-rules/

      希望这会有所帮助:-)

      【讨论】: