本文介绍了.htaccess,强制使用斜杠和 www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can someone please help me to force my website to redirect to using www. and ALWAYS add a slash at the end of any page? My htaccess file currently looks like this:

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]+)

非常感谢:)

推荐答案

这适用于任何域:

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

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

This will add the slash when necessary. (%{REQUEST_URI} will be / with a request for root, or /whatever if you request domain.com/whatever.) It also won't put in two slashes (//) for the main domain as the other solution does. The DirectorySlash directive ensures the slash is added where appropriate even if the www is already present.

这篇关于.htaccess,强制使用斜杠和 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:32