Today I moved my wordpress website from Root Directory to Subdirectory in same domain. I moved all files to new subdirectory. I have also setup 301 wildcard redirection from Hosting cpanel
301 Wildcard redirection Setting -
Old Address - https://www.example.com
New Address - https://www.example.com/blog/
When I am visiting any post directly like https://www.example.com/blog/this-is-a-post
then it showing correct post without any error but the problem when I am opening the same post without "blog" word i.e. https://www.example.com/this-is-a-post
then it also showing correct post instead of redirecting it to https://www.example.com/blog/this-is-a-post
.
And when I visit through Google then it visiting the old address only i.e. without the word "blog".
One more thing is occuring is redirecting https://www.example.com
to https://www.example.com/blog
which is also what I don't want because I have different html index file for homepage
URL Settings in Wordpress Dashboard -
WordPress Address (URL) - https://www.example.com/blog
Site Address (URL) - https://www.example.com/blog
Contents of .htaccess
file:
# BEGIN WordPress
<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]
</IfModule>
# END WordPress
After removal of .htaccess and index.php file of wordpress from Root directory and removal of 301 redirect from cpanel.. The problem is fixed. Now I have different welcome page in roodex.com and have wordpress blog in roodex,com/blog But Now the problem is When visit through Google search result then Google showing old post url links and after visiting through that it is showing "Not Found" instead of redirecting to new url.
Assuming you are only serving static files from the root of your site (ie. you don't have virtual URLs and a front controller like WordPress) then the best you can do is redirect any request for the root directory (outside of /blog
) that does not map to a physical file, to your /blog
subdirectory and hope for the best. If the URL doesn't exist within WordPress either then you'll just get WordPress's 404 page.
I assume you have a .htaccess
file in the /blog
subdirectory that handles the WordPress requests?
So, in the .htaccess
file in the root of your site (which I believe is now empty), try the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !blog/ /blog%{REQUEST_URI} [R=302,L]
Change the 302
(temporary) redirect to a 301
(permanent) when you are sure this is working OK. 301s are cached hard by the browser so can making testing problematic.