PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

[FIXED] how to redirect browser to two different site with apache virtualHost?

 September 20, 2022     apache, virtualhost     No comments   

Issue

I have a valid IP, e.g. x.x.x.x, and a domain, e.g. site.com which point to x.x.x.x.

First, I need to redirect any request from port 80 to 443 (security issue). Second, for every request sent to my IP, I want apache to show "Hello it's working" (/var/www/index.html) and everyone request to my domain to show the real site.

I tried this, but it did not work:

<VirtualHost x.x.x.x:433>
    ServerName x.x.x.x
    ServerAlias x.x.x.x
    DocumentRoot /var/www/
    DirectoryIndex index.html
    Options -Indexes
</VirtualHost>

<VirtualHost site.com:443>
...
</VirtualHost>

Solution

for redirection:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.yourdomain.com
   Redirect / https://www.yourdomain.com
</VirtualHost>

<VirtualHost _default_:443>
   ServerName www.yourdomain.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

for domain separation:

<VirtualHost *:80>
    # This first-listed virtual host is also the default for *:80
    ServerName www.example.com
    ServerAlias example.com 
    DocumentRoot "/www/domain"
</VirtualHost>

<VirtualHost *:80>
    ServerName other.example.com
    DocumentRoot "/www/otherdomain"
</VirtualHost>


Answered By - H00man
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing