Apache httpd.conf for redirecting ip to hostname

后端 未结 6 1965
执念已碎
执念已碎 2020-12-25 14:18

I have external IP and hostname configured for my machine.

Inside the application, I am using only the domain names to access the APIs. So when i try to access my AP

相关标签:
6条回答
  • 2020-12-25 14:46

    Try this:

    RewriteRule $ https://ayz-abc.mysite.com/ [L,R]
    

    Also you can see rewrite logs, see here

    0 讨论(0)
  • 2020-12-25 14:47

    If you are using https domain than add following:

    # BEGIN HTTPS Redirection Plugin
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^XX\.XX\.XX\.XX$
    RewriteRule ^(.*)$ https://your-domain-name.com/$1 [L,R=301]
    </IfModule>
    # END HTTPS Redirection Plugin
    

    We are using https redirection plugin on site. Above block of code is generated by the plugin and we added only two lines in the code block. Below two lines are responsible to set IP to Domain redirection:

    RewriteCond %{HTTP_HOST} ^XX\.XX\.XX\.XX$
    RewriteRule ^(.*)$ https://your-domain-name.com/$1 [L,R=301]
    

    Here ^XX.XX.XX.XX$ is your IP address, replace it like this: ^12.34.56.78$ with your server IP.

    Thanks

    0 讨论(0)
  • 2020-12-25 14:50

    This will redirect all request to IP to the similar on your domain

    <IfModule mod_rewrite.c>
    
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^12\.34\.56\.78$
    
    RewriteRule ^/(.*)$  https://ayz-abc.mysite.com/$1 [L,R=301]
    
    </IfModule>
    
    0 讨论(0)
  • 2020-12-25 14:52

    Ok. You are missing a rewrite condition

    <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

    0 讨论(0)
  • 2020-12-25 14:52

    If you are NOT using API but just want browsers and crawlers to go to URL instead of an IP-Address, then you can use RedirectPermanent.

    <VirtualHost XX.XX.XX.XX>
    
        RedirectPermanent / http://ayz-abc.mysite.com/
    
    </VirtualHost>
    
    <VirtualHost XX.XX.XX.XX>
    
        DocumentRoot "/var/www/html"
        ServerName ayz-abc.mysite.com/
    
    </VirtualHost>
    

    It has the advantage of responding with 301 HTTP status, which signals "please use the url you are redirected to in the future", which helps with search engines. You should use the same solution if you move your site to a new domain.

    0 讨论(0)
  • 2020-12-25 15:03

    This works for me. Add the configurations in httpd.conf of apache

    CASE-1: when user hits http://XX.XX.XX.XX/main or http://ayz-abc.mysite.com/main it should be redirected to https://ayz-abc.mysite.com/main

    Configuration:

    #
    # Use name-based virtual hosting.
    #
    NameVirtualHost *:80
    <VirtualHost *:80>
    ServerName XX.XX.XX.XX
    Redirect /main https://ayz-abc.mysite.com/main
    </VirtualHost>
    

    CASE 2 : When user hits https://XX.XX.XX.XX/main it should be redirected to https://ayz-abc.mysite.com/main

    Configuration:

    NameVirtualHost *:443
    <VirtualHost *:443>
    DocumentRoot "/var/www/html"
    #Server Name 
    ServerName XX.XX.XX.XX
    SSLEngine on
    SSLOptions +StrictRequire
    # Redirect to the specified URL 
    Redirect /main https://ayz-abc.mysite.com/main
    <Directory />
    SSLRequireSSL
    </Directory>
    ....
    ....
    </VirtualHost>
    
    0 讨论(0)
提交回复
热议问题