.htaccess, forcing a slash and www

2020-02-02 02:18发布

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

Many thanks in advance :)

标签: .htaccess
2条回答
做个烂人
2楼-- · 2020-02-02 02:47

I do not know how to add the trailing slash, but adding www in front of URLs are quite simple. Here's what im using to add the www before an url:

RewriteEngine on

Options FollowSymlinks

rewritecond %{http_host} ^yourdomain.com [nc]

rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]

Googled for the trailing slash-thingy and found this article: http://www.mydigitallife.info/2007/03/19/add-trailing-slash-to-the-end-of-the-url-with-htaccess-rewrite-rules/

Hope this helps :-)

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-02 02:50

This will work for any domain:

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

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.

查看更多
登录 后发表回答