大网数据广告活动
服务器运维问答

Redirect web page to another site without changing URL

· 52CE 我爱测

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 wit…

问题原文

原帖链接 · Christia · CC BY-SA 4.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.

已采纳解答

原帖链接 · Nikita Kipriyanov · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。