URL重写了在.htaccess不同协议写了、协议、不同、URL

由网友(为何久不归)分享简介:我需要在.htaccess URL重写帮助。I need help with url-rewriting in .htaccess.所以,问题是关于不同的协议:HTTPS和HTTP。重写的主要目的是从URL中删除www的,但协议应该保持相同它是前So the issue is about different pr...

我需要在.htaccess URL重写帮助。

I need help with url-rewriting in .htaccess.

所以,问题是关于不同的协议:HTTPS和HTTP。重写的主要目的是从URL中删除www的,但协议应该保持相同它是前

So the issue is about different protocols: https and http. The main purpose of rewriting is to remove "www" from URL, but protocol should stay the same it was before.

例如,当我有URL例如 HTTP ://www.domain.com/request,它应该被重定向到 HTTP ://domain.com /请求。我决心用这些规则:

For example, when I have URL like http://www.domain.com/request, it should be redirected to the http://domain.com/request. I resolve it with these rules:

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

但在情况下,当URL看起来像 HTTPS ://www.domain.com/request它应该被重定向到 HTTPS ://domain.com/request

But in case, when URL looks like https://www.domain.com/request it should be redirected to https://domain.com/request.

不幸的是,上述规则将重定向为 HTTP 不论当时的协议。

Unfortunately, the above rule will redirect to http regardless current protocol.

在此先感谢。

推荐答案

这是非常相似的链接可能重复,但由于一个部队 WWW 要删除它,它可能需要建立单独的答案。

This is fairly similar to the linked possible duplicate, but since that one forces www where you want to remove it, it might warrant a separate answer.

尝试是这样的:

RewriteCond %{HTTP_HOST} ^www.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
阅读全文

相关推荐

最新文章