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

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

问题描述

我需要有关 .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.

例如,当我有像 http://www.domain.com/request 这样的 URL 时,它应该被重定向到 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]

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

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