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/
2
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.

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]
-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]
3
  • 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 5 '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. Mar 5 '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 5 '18 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.