skip to Main Content

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?

2

Answers


  1. You can not achieve this with a Redirect as it appends the old URL path to new one. You can use RewriteRule

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^sub.example.com$ [NC]
    RewriteRule (.*) https://example.com/ [R=301,L]
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search