Score:1

htaccess: 301 redirect defined URLs to new domain and catch all redirect the rest

io flag

I want to redirect some URLs from an old domain to new versions on a new domain. The old domain uses the www. subdomain and SSL.

For example:

  • olddomain.com/audi -> newdomain.com/cars/audi
  • olddomain.com/bmw -> newdomain.com/cars/bmw

But there are also a lot of pages/URLs on the old domain with no new version of them. For these pages and all URLs with the www. subdomain I want to use a "catch all" redirect to the homepage.

This is what I'm using now:

# redirect specific pages
RewriteEngine on
Redirect 301 /audi https://www.newdomain.de/cars/audi/

#redirect everything else
RewriteRule ^(.*)$ http://www.newdomain.de/ [R=301,L]

Unfortunately it's not working all pages are redirected to the homepage.

kz flag
I'm assuming the domains point to different servers or areas of the filesystem?
Score:2
kz flag

Redirect is a mod_alias directive (nothing to do with RewriteEngine). RewriteRule belongs to mod_rewrite. mod_rewrite is always processed first, despite the apparent order of the directives in the config file. Consequently you should not mix redirects from both modules as you can get unexpected results, due to the order of processing.

You either need to use mod_rewrite for everything. For example:

RewriteEngine on

# Redirect specific pages
RewriteRule ^audi$ https://www.newdomain.de/cars/audi [R=301,L]

# Redirect everything else
RewriteRule ^ http://www.newdomain.de/ [R=301,L]

You can combine the /audi and /bmw redirects in a single directive. For example:

RewriteRule ^(audi|bmw)$ https://www.newdomain.de/cars/$1 [R=301,L]

Where $1 is a backreference to the captured group in the RewriteRule pattern.

OR, use mod_alias for everything (providing there are no other conflicts). For example:

# Redirect specific pages
Redirect 301 /audi https://www.newdomain.de/cars/audi

# Redirect everything else
RedirectMatch 301 ^/ http://www.newdomain.de/

I removed the slash suffice on the target URL as it's not present in your example.

Note that we need to use RedirectMatch (also a mod_alias directive) for the "everything else" redirect since Redirect is prefix-matching and would otherwise redirect to the same URL at the target (not the homepage).

As noted above, Redirect is prefix-matching, so the first Redirect directive would actually redirect /audi/something to /cars/audi/something. If that is an issue then use RedirectMatch instead.

You should test first with 302 (temporary) redirects to avoid potential caching issues and you will need to clear your browser (and any intermediary) caches before testing, since the erroneous redirect to the homepage will likely have been cached by the browser.

Aside: Note that mass redirects to the homepage are likely seen as soft-404s by the search engines (notably Google). It is often preferable (for search engines and users) to serve a more meaningful custom 404 with additional links to pages you want to promote.

Reference:

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.