本文介绍了Apache httpd.conf 用于将 ip 重定向到主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的机器配置了外部 IP 和主机名.

I have external IP and hostname configured for my machine.

在应用程序内部,我仅使用域名来访问 API.因此,当我尝试通过 IP 地址访问我的 API 时,它显示 302 Moved 临时错误.因此,对于使用 IP 地址访问服务器的请求(对于主页),它应该重定向到主机名.

Inside the application, I am using only the domain names to access the APIs. So when i try to access my APIs through IP address, it shows 302 Moved temporarily error.So, for request(for Homepage) that hits the server with IP address, It should redirect to hostname.

也就是说,当用户点击 https://XX.XX.XX.XX/main 时,它应该被重定向https://ayz-abc.mysite.com/main

That is, when user hits https://XX.XX.XX.XX/main it should be redirected to https://ayz-abc.mysite.com/main

为此,我尝试在 apache 的 httpd.conf 中使用重定向.

For this I tried using the redirect in httpd.conf of apache.

<VirtualHost XX.XX.XX.XX>

DocumentRoot "/var/www/html"
#ServerName ayz-abc.mysite.com/

 # Other directives here
 RewriteEngine On
 RewriteRule /.* https://ayz-abc.mysite.com/ [R]

</VirtualHost>

我也尝试过以下内容

<VirtualHost *.portnum>
DocumentRoot "/var/www/html"
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule  https://XX.XX.XX.XX/main https://ayz-abc.mysite.com/main [R=301,L]
</VirtualHost>

请帮帮我.

推荐答案

好的.您缺少重写条件

<VirtualHost XX.XX.XX.XX>

DocumentRoot "/var/www/html"
#ServerName ayz-abc.mysite.com/

 # Other directives here
 RewriteEngine On
 RewriteCond %{HTTP_HOST} !^ayz-abc.mysite.com$
 RewriteRule /.* https://ayz-abc.mysite.com/ [R]

</VirtualHost>

如果您不包含此条件,即使使用主机名也会重定向

If you don't include this condition it will redirect even with the hostname

这篇关于Apache httpd.conf 用于将 ip 重定向到主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 18:30