Search code examples
apache.htaccesshttp-redirect

301 redirect (.htaccess) of entire domain to another domain index


I have tried solving this on my own but I can't seem to figure it out.

Here is what I'm trying to do:

Create a 301 redirect of all URLs (including index) on subdomain.example.com to example.com.

So for example:

  • subdomain.example.com redirects to example.com
  • subdomain.example.com/blog-post redirects to example.com

This means that I DO NOT want to keep the same URL structure that's used on subdomain.example.com. I just want URLs on the subdomain to point to the index on the example.com. I know that's not the best solution for SEO, but that's how I want it.

I have tried accomplish this using

Redirect 301 / https://example.com/

but that keeps the URL structure and I don't want that.

So how can I solve this?


Solution

  • according to the documentation https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect

    keeps the part of the url.

    With redirectMatch you can use regexes and substitution to rewrite your url e.g.

    RedirectMatch 301 . https://example.com/
    

    where the dot (.) is a match anything.