阿帕奇重写规则,以消除任何域名端口 [英] Apache RewriteRule to remove port on any domain name

查看:133
本文介绍了阿帕奇重写规则,以消除任何域名端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IP地址的虚拟服务器,提供了几个不同的网站。他们中的一个上有一个SSL证书。

I have a virtual server with one IP address, serving a few different sites. One of them has an SSL certificate on it.

我需要通过自己对供私人使用的第二域添加SS​​L证书,并作为我只有一个IP地址,我加了它在端口8080来代替。这工作得很好。

I needed to add an SSL cert on a second domain for private use by myself, and as I only have one IP address, I added it on port 8080 instead. This works fine.

我现在的问题是,所有指向该服务器的域会显示我的私人网站,如果8080端口要求在该领域。

My problem now is that all the domains that point to the server will display my private site if port 8080 is requested on that domain.


https://example1.com:8080/ - >将显示我的私人网站,但不应该
https://example2.com:8080/ - >将显示我的私人网站,但不应该
https://desired-domain.com:8080/ - >将显示我的私人网站,这是正确的!

任何人访问 https://example1.com:8080/ 应该被重定向到 http://example1.com/ ,与同为 example2.com

Anyone visiting https://example1.com:8080/ should be redirected to http://example1.com/, and the same for example2.com.

我想...


的RewriteCond%{HTTP_HOST}!希望\\ -domain \\ .COM [NC]


重写规则^ $ HTTP(*)://%{HTTP_HOST} $ 1 [L,R = 301]

...但端口被附加到HTTP_HOST,所以它出来为 http://example1.com:8080/

...but the port is appended to HTTP_HOST, so it comes out as http://example1.com:8080/

所以,总之,我的问题:我怎样才能重定向到被请求的主机,却忽略了请求端口

So, in short, my question: how can I redirect to the requested host, but ignore the request port?

EXTRA

EXTRA

我已经试过这...


的RewriteCond%{HTTP_HOST}(desired-domain.com):8080 $ [NC]


(。*)重写规则^ $的http://%1 $ 1 [L,R = 301]

...但它重定向到的http://本地主机/ 。这是不是显示错了网站的页面更好,不过,因为没有人应该在任何其他域请求端口8080反正!

...but it redirects to http://localhost/. That's better than showing the wrong site's page, though, as nobody should be requesting port 8080 on any other domain anyway!

我怀疑有一个更好的desired-domain.com正则表达式,但我的正则表达式,魔力今天不流淌。

I suspect there's a better 'desired-domain.com' regex, but my regex-mojo isn't flowing today.

推荐答案

也许尝试:

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

显然,你需要有点聪明关于您HTTP_HOST常规的前pression,如果有一个正则表达式中封装的例1和例2域没有简单的方法,你可以有那么2,使用 [OR]

RewriteCond %{HTTP_HOST} ^(example1.com):8080$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(example2.com):8080$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]


编辑:

您所无法比拟的,比如反向引用(desired-domain.com)!8080 $ ,作为 意味着它的不匹配的。但你可以尝试这样的事:

You cannot match a backreference like !(desired-domain.com):8080$, as the ! means that it doesn't match. But you can try something like this:

RewriteCond %{HTTP_HOST} !^desired-domain.com(:8080)?$ [NC]
RewriteCond %{HTTP_HOST} ^(.*):8080$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

这篇关于阿帕奇重写规则,以消除任何域名端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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