0

I have a domain name (example2.com) that is going to serve to direct a user to a specific page of another website on the same server (example.com/page2). I am following the instructions in this article for creating a redirect without changing the URL, see example 2, but must be doing something wrong because the affect I am getting is either an error message or it is loading the dummy content on the index.html. When I delete the index.html, I get:

Not Found The requested URL / was not found on this server" as one would expect with no index.html.

I have set up a new vhost in apache by creating an example2.com.conf in sites-available and created a symlink in sites-enabled. I created a dummy index.html and defined the docroot in the example2.com.conf file. All good so far, the dummy content loads.

The entire contents of my .htaccess file in the docroot of example2.com are as follows:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example2.com
RewriteRule ^(.*) http://www.example.com/page2 [P]

Any links on the page that I want the domain to load are either external, or are allowed to go to example.com as normal. It's an unbranded partner project page, built using Drupal. The domain name (example2.com) needs to load (example.com/page2) but show example2.com in the address bar. This is the only content for the domain name so I only created a docroot for it for the sake of testing dummy content and creating the .htaccess.

Apache and Debian 9.

I've also tried some suggestions from this post, many of which say you can't do it using a .htaccess file. I've also tried the suggestions in the answers below.

https://stackoverflow.com/questions/987343/how-can-i-redirect-to-a-different-domain-without-changing-the-url-in-the-address

Note: this question has had a major rewrite as I've gotten closer at at explaining what I'm trying to do.

3
  • 2
    Using mod_rewrite. Jun 25, 2018 at 6:04
  • Yes, but how should I configure it so that it's not parking the domain to the entire website but just one page?
    – Christia
    Jun 25, 2018 at 6:34
  • 2
    I'm trying my best to explain this, why the down vote?
    – Christia
    Jun 25, 2018 at 8:08

2 Answers 2

1

You can. There are several options to do this.

The one way is to configure a true virtual domain example2.com and set up only Redirect option to redirect everything to where you want. This is actually simpler and lighter approach (mod_rewrite is much more "heavy artillery").

I.e.

<VirtualHost *:80>
ServerName example2.com
Redirect / http://example.com/page2
</VirtualHost>

DigitalOcean seem to recommend this approach.

The actual redirect condition could be changed of course.

The other way is to learn how to configure mod_rewrite. Provided your mod_rewrite is correctly loaded and your configuration part will actually receive the request (i.e. correctly set up VirtualHost or main server), also there are none other mod_rewrite directives to intervene with this one, you can use something like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/page2/$1 [L,R]

To load only main page there you can replace the .* match condition in the RewriteRule, i.e. to capture only index page ("empty request"):

RewriteRule ^$ http://example.com/page2 [L,R]

(or shouild it be ^/$? I don't know, try both).

https://stackoverflow.com/questions/13021893/htaccess-mod-rewrite-hostname

Note the behaviour of there two options differ. The first one will tell the user agent to load another URL, so user could notice they moved to another address. The second one will present to user the content of other address as if it was located at the first place, i.e. user will see example2.com domain name, but the page will come from example.com/page2. They have no way to know if this page is available at the other addres. So, the page contains included media (css, images, fonts) you have to provide rewrite rules for them too or use only absolute URIs for them.

I personally recommend to follow Apache developers and to avoid mod_rewrite if it's possible (also see this answer).

See official documentation of the mod_rewrite also. Be ware, this module is feature, so use knowingly and with caution.

6
  • Thank you. I like the light approach. I see that you have example2.com for the server name. Do I need to define this somewhere else so Apache will recognize it? Or is that what this is essentially doing?
    – Christia
    Jun 25, 2018 at 6:49
  • 2
    No, you souldn't. This directive (ServerName inside a VirtualHost) is the actual place you tell Apache what to do with if request it received contains this host name. It tells it to use further configuration from this virtualhost, rather than from main server or other virtualhosts. Jun 25, 2018 at 6:53
  • 1
    This answer is about redirects, alone. If the sites are both on the same server, it's possible to show the same page without redirection. Otherwise, you'd need to set up a reverse proxy to keep the URL unchanged. Jun 25, 2018 at 7:22
  • You mention "both sites". I didn't have anything set up for the new domain. What should I put in the docroot?
    – Christia
    Jun 26, 2018 at 23:51
  • 1
    You haven't need to put anything there. It won't be used at all, because any request will be redirected. You'll only create docroot directory to make webserver happy and not complain. Jul 12, 2018 at 8:01
3

If you want to keep the URL while showing a page from different domain, you need a reverse proxy.

  • mod_proxy is the most straight-forward solution, as described in Reverse Proxy Guide.

    <VirtualHost *:80>
        ServerName example.net
        ProxyPass "/"  "http://www.example.com/page2/"
        ProxyPassReverse "/"  "http://www.example.com/page2/"
    </VirtualHost>
    
  • mod_rewrite RewriteRule also has a proxy flag you can use to avoid redirection:

    proxy P: Force the substitution URL to be internally sent as a proxy request.

    RewriteCond %{HTTP_HOST} ^example.net
    RewriteRule ^(.*) http://example.com/page2 [P]
    

If the site is on the very same server, it should be possible to point the DocumentRoot directly, too:

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

<VirtualHost *:80>
    ServerName example.net
    DocumentRoot "/var/www/page2"
</VirtualHost>
3
  • All these are doing exactly what you are trying to achieve, but if they are on the same server, using the DocumentRoot will have best performance. Jun 25, 2018 at 7:57
  • Where else all the configuration would go? It is a server. A site without a server...there's no such thing. Whatever is serving the site, proxy or even simple redirection is by definition a server. Jun 25, 2018 at 8:13
  • 1
    Yes. My answer is exactly for that. Jun 25, 2018 at 9:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .