.htaccess subdomain redirect + wordpress

How do I enable this:

If I type: maggew.com/abcxyz
you’ll get redirected to mr.maggew.com/abcxyz

Read More

If I type: maggew.com/privacy
you’ll get redirected to mr.maggew.com/privacy

if I type: maggew.com/refund
you’ll get redirected to mr.maggew.com/refund

If I type: maggew.com/contact
you’ll get redirected to mr.maggew.com/contact

etc..etc…etc….

I was using Shared Hosting on 1&1, and now I have a digital ocean droplet.
I tried to use HTACCESS REDIRECT GENERATOR but didn’t have much luck.

Here is my current .htaccess:

# REDIRECTS STORE
redirect 301 /shop http://mr.maggew.com/store
redirect 301 /market http://mr.maggew.com/store
redirect 301 /outlet http://mr.maggew.com/store
redirect 301 /bazaar http://mr.maggew.com/store
redirect 301 /boutique http://mr.maggew.com/store

# PROTECT WP-CONFIG.PHP
<files wp-config.php>
order allow,deny
deny from all
</files>

RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
RewriteRule . index.php [L]

#force subdomain of mr.maggew.com
RewriteCond %{HTTP_HOST} !^mr.
RewriteRule (.*) http://mr.maggew.com%{REQUEST_URI} [R,L,QSA]

Related posts

Leave a Reply

3 comments

  1. Option 1

    before you do all the folder specific redirects you need to redirect the domain first
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^http://www.domain.com/ [NC]
    RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
    

    Option 2

    if you have old url instances in your content you can use something like this to update to the new one. I’m not sure how effective this is.

    http://www.wpbeginner.com/plugins/how-to-update-urls-when-moving-your-wordpress-site/

    Option 3

    or you can use http://wp-cli.org/ modify database instances or you can use mysql command line search and replace.

    all of your urls from content and page referencing urls are stored in the database. you can do a mass search and replace if you know what you’re doing.

  2. Assuming you are on the same host and directory structure, you can add a condition checking for the subdomain in the HTTP_HOST, if the subdomain is missing you can redirect to it.

    # REDIRECTS STORE
    redirect 301 /shop http://mr.maggew.com/store
    redirect 301 /market http://mr.maggew.com/store
    redirect 301 /outlet http://mr.maggew.com/store
    redirect 301 /bazaar http://mr.maggew.com/store
    redirect 301 /boutique http://mr.maggew.com/store
    
    # PROTECT WP-CONFIG.PHP
    <files wp-config.php>
    order allow,deny
    deny from all
    </files>
    
    RewriteEngine On
    #force subdomain of mr.maggew.com
    RewriteCond %{HTTP_HOST} !^mr.
    RewriteRule /?(.*)$ http://mr.maggew.com/$1 [R,L,QSA]
    
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
    RewriteRule . index.php [L]
    

    Alternative

    #force subdomain of mr.maggew.com
    RewriteCond %{HTTP_HOST} !^mr.
    RewriteRule (.*) http://mr.maggew.com%{REQUEST_URI} [R,L,QSA]