Centos – Best way to redirect pages on an apache/centos web server

apache-2.2centosredirectweb-hostingweb-server

I have a centos 6.5 web server running 2 websites off 1 ip address by using virtual hosts.

domain1.com and domain2.com – both hosted on the same web server above.

I need to redirect about 40 pages from domain1 to domain2, "for example":

domain1.com/page1 -> domain2.com/new-page-1
domain1.com/welcomepage -> domain2.com/new-welcome-page
domain1.com/johnsmith -> domain2.com/elvis
domain1.com/test -> domain2.com/production

*Notice that the pages I am redirecting are not under the same structure/name, they go to a completely different structure/name.

Can anyone suggest what I can/need to do to accomplish this task?

EDIT #1
I tried doing this through the VirtualHost section in my httpd.conf file. See below for my entries.

<VirtualHost *:80>
 ServerName domain1.com
 ServerAlias www.domain1.com
 RedirectPermanent / http://www.domain2.com/page12345/
</VirtualHost>

<VirtualHost *:80>
 ServerName domain1.com
 ServerAlias www.domain1.com
 RedirectPermanent /AboutUs/Founders http://www.domain2.com/about-us-founders/
</VirtualHost>

In the above two entries, only the first one is working right and redirecting correctly. The second one is redirecting to: http://www.domain2.com/page12345/AboutUs/Founders
Any ideas?

Best Answer

The simplest solution if often the best in such cases; add 40 Redirect directives to the domain1 VirtualHost configuration, where the only choice you need to make is on the permanent or temporary status of the redirect:

<VirtualHost *:80>
   Servername domain1.com
   RedirectTemp /page1 http://domain2.com/new-page-1
   RedirectPermanent /welcomepage http://domain2.com/new-welcome-page
</VirtualHost>

In response To the Edit #1 above:


When you use multiple VirtualHost stanza's with the same domain names in either the ServerName or ServerAlias only the first one is valid and the subsequent ones will be ignored.

A single VirtualHost stanza can hold multiple Redirect directives, so move the second Redirect directive to the first virtualhost stanza and delete the second.

Second reading the manual in the link above really helps:

Any request beginning with URL-path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-path will be appended to the target URL.
Example: Redirect /service http://foo2.example.com/service
If the client requests http://example.com/service/foo.txt, it will be told to access http://foo2.example.com/service/foo.txt

That corresponds exactly with what you observed with your requests for www.domain1.com/AboutUs/Founders which trigger RedirectPermanent / http://www.domain2.com/page12345/ that redirects the original request to www.domain2.com/page12345/AboutUs/Founders

You can resolve that by ordering the Redirect lines correctly, because Apache will process the Redirect directives in order. Start with the longest URL path's because otherwise they're caught by a valid redirect on a shorter directory.

<VirtualHost *:80>
   Servername domain1.com
   Redirect /AboutUs/Founders http://www.domain2.com/about-us-founders/
   Redirect /AboutUs/         http://www.domain2.com/about-us/
   Redirect /index.html       http://www.domain2.com/page12345/
   RedirectMatch ^            http://www.domain2.com/page12345/
</VirtualHost>

For Redirecting requests that consist of only http://domain1.com you use a ^ rather then / often it is a good idea to also explicitly redirect the IndexDocument as well, hence the /index.html entry.

Related Topic