URL重写了在.htaccess不同协议 [英] URL rewriting for different protocols in .htaccess

查看:251
本文介绍了URL重写了在.htaccess不同协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在.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]

这篇关于URL重写了在.htaccess不同协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆