0

I have a subdomain

http://blog.example.com

I would like to redirect it to subdirectory as

https://www.example.com/blog

But not admin(/wp-admin). Admin area should be as it is

http://blog.example.com/wp-admin

currently I have this in my htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /blog/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]

    RewriteCond %{HTTP:X-Forwarded-Host}i ^example\.com 
    RewriteCond %{HTTP_HOST} ^blog\.example\.com$  
    RewriteRule ^/?(.*)$ https://www.example.com/blog/$1 [L,R=301,NC]

    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^/?(.*)$ https://www.example.com/blog/$1 [L,R=301,NC]
</IfModule>

and my site URL and WordPress URL is:

WordPress Address (URL)   = /blog 
Site Address (URL)        = https://www.example.com/blog

but this setup redirects every URLs in my website and creating many issues in the backend.

What is the best ways to do this?

1
  • That won't be easy. You have a lot of files in other folders such as wp-includes that are loaded both in admin and front end.
    – Johansson
    Commented Apr 2, 2017 at 16:35

2 Answers 2

2

Use below rule,

RewriteEngine On

# excluding www
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^blog

# excluding wp-admin
RewriteCond %{REQUEST_URI} !^wp-admin
RewriteRule ^ https://www.example.com/%1
1
  • 1
    Hey, Abhishek Gurjar nice code. Your answer is off to a good start. Please consider explaining how and why this code solves the problem so others can learn from it. Check the guide to answers for further advice. Commented Sep 2, 2019 at 20:51
0

Have you tried the following?

<html>
    <head>
        <meta http-equiv="refresh" content="0; url=https://www.example.com/blog" />
    </head>
</html>

This would be your index.php file, and you would place it in http://blog.example.com

Note The 0 in the refresh statement is the number of seconds your page waits before redirecting to a new site, and 0 means redirect immediately.

Note 2 You can make a new theme, that only contains this index.php file above (and the style.css file for theme definition), and switch to this theme, and you will be set. Alternatively, update your existing theme, and add the meta tag above to your header. Either way should work.

2
  • Thank you @Greeso. It is working. But the includes( js and css ) are still loading from subdomain(blog.example.com). so the page seemed to be broken. Also when we try directly "example.com/blog" this URL it is redirecting infinitely. Commented Apr 3, 2017 at 10:59
  • First, can you go directly to example.com/blog ? If this does not work, then you have a problem. If it works, then try to create a new theme, containing only index.php and style.css , and you index.php would only contain the contenct in the answer above (so nothing is loaded other than this file). See how this works.
    – Greeso
    Commented Apr 3, 2017 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.