1

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.

1
  • I'm assuming the domains point to different servers or areas of the filesystem?
    – MrWhite
    Jan 5 at 10:56

1 Answer 1

1

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:

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.