2

How do I 301 specific pages to a new domain URI, and then all the rest of the pages, just send to the homepage?

Example

-- Specific pages I want to move -- 

Redirect 301 /contact.htm https://newdomain.com/contact
Redirect 301 /about.htm https://newdomain.com/about/
Redirect 301 /team.htm https://newdomain.com/team/

-- All other pages, just redirect to the homepage --

Redirect 301 /whatever.htm https://newdomain.com/
Redirect 301 /blah.htm https://newdomain.com/
Keith Donegan
  • 121
  • 1
  • 3
  • 3
    Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Tero Kilkanen Mar 05 '18 at 23:40
  • 2
    Although flagging this **mod_alias** question as a duplicate of the catch-all **mod_rewrite** question would seem to be sending the wrong message? (The "problem" is that the catch-all question does not even mention mod_alias or the `Redirect` directive? And mod_rewrite is probably not the correct tool here.) – MrWhite Mar 11 '18 at 17:22

3 Answers3

3

To match all remaining pages and redirect to the home page of the new site you'll need to use the RedirectMatch directive (also from mod_alias). For example:

RedirectMatch 301 .* https://newdomain.com/

The RedirectMatch directive uses regex to match the request URL, whereas Redirect uses simple prefix matching.

You can't use a Redirect directive here, which is prefix matching, because whilst a redirect such as Redirect / https://newdomain.com/ will match all the remaining URLs, it will redirect to the same URL-path at newdomain.com. eg. /whatever.htm would redirect to https://newdomain.com/whatever.htm (which presumably doesn't exist - although that might actually be a good thing - since mass redirects to the home page will be seen as a soft-404 anyway by Google and a real 404 can be more informative for users).

Just to add, this does assume that newdomain.com is hosted on a different server, otherwise, you'll get a redirect loop.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
0

htaccess runs in cronological order, so you have to write the rules from up to down.

Specific pages first, than the last row will be the "other pages to the home page" rule.

In the example the first rule redirects the homepage only - in case of /index.html for example, the last row will run if other rules are not maches to the url - like other pages which are not defined above in the htaccess. You can use the whole code, because it is ready to use.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

### 301 redirect ###
#old homepage to new homepage
RewriteRule ^$ http://yourhomepage.com/ [L,R=301]

#specific pages to new specific pages
RewriteRule ^contact.html?$ http://yourhomepage.com/contact [L,R=301]
RewriteRule ^index.html?$ http://yourhomepage.com/ [L,R=301]

#other pages to the new website's homepage
RewriteRule ^(.*)$ http://yourhomepage.com/ [L,R=301]
Imre
  • 101
-1

You should use RewriteRule for all the non matched url's. You can also use mod_rewrite for all the previous redirects.

Something like this would probably work.

RewriteRule ^(.*)?$ /index.html [R=301,L]
Gothrek
  • 512
  • 2
  • 7
  • 1
    This `RewriteRule` (mod_rewrite) will conflict with the existing mod_alias `Redirect` directives that redirect the specific pages. The result is that _everything_ will be redirected to `/index.html`, including the "specific pages". (Incidentally, you'd also need to specify the `newdomain.com` in the _substitution_.) – MrWhite Mar 05 '18 at 11:39
  • @MrWhite: Actually it's possible to use mod_rewrite and mod_alias simultaneously in the `.htaccess` context, as suggested. You just need to have the `Redirect` directives before the `RewriteRule` so that they get matched first. But still, your `RedirectMatch` solution is much more simple and elegant for such a simple task. – Esa Jokinen Mar 05 '18 at 11:51
  • 1
    @EsaJokinen The order doesn't matter, the mod_rewrite `RewriteRule` directive will _always_ execute before the mod_alias `Redirect` directive. For this to work, you would need to change the existing `Redirect` directives to `RewriteRule` as well. – MrWhite Mar 05 '18 at 11:54