Home > Back-end >  Replace blog with static page redirect rest to /blog with htaaccess
Replace blog with static page redirect rest to /blog with htaaccess

Time:10-26

I have a website that currently has a WordPress blog. I want to move the blog into /blog and make the static page the new "root".

That's easy in terms of FTP, just moving folders along.

However, I'd love to get anything that would normally be 404 to get redirected to the /blog part so content doesn't get lost on the migration.

Is this doable with .hatccess?

Current .htaccess file:

#DirectoryIndex index.php index.html

#Options  FollowSymLinks

# Indexes
Options All -Indexes

# REDIRECT https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

# BEGIN WordPress
# Dynamically generated by WP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# BEGIN FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
# END FileETag MTime Size<!--formatted-->

# Protecting htaccess
<files .htaccess>
order allow,deny
deny from all
</files>

# Protecting wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>%

CodePudding user response:

Try using the ErrorDocument directive

ErrorDocument 404 /blog

CodePudding user response:

Assuming the "current .htaccess file" you have posted is the .htaccess file you intend to move to /blog/.htaccess. In which case you will need to change it as follows:

#DirectoryIndex index.php index.html

#Options  FollowSymLinks

# Indexes
Options All -Indexes

# REDIRECT https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

# BEGIN WordPress
# Dynamically generated by WP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

# END WordPress

# BEGIN FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
# END FileETag MTime Size<!--formatted-->

# Protecting htaccess
<files .htaccess>
order allow,deny
deny from all
</files>

# Protecting wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>

Notable changes:

  • Changed HTTP to HTTPS redirect to use the REQUEST_URI server variable instead of a backreference. With it being in a subdirectory (blog), the subdirectory would otherwise be omited from the redirect to HTTPS.

  • Commented out (removed) the RewriteBase directive. This is not required here, but if you did need to set this, it should be set to RewriteBase /blog.

  • Removed the slash prefix on the RewriteRule substitution string. ie. Changed this RewriteRule . /index.php [L] to this RewriteRule . index.php [L]

    Without the RewriteBase defined, this is now relative to the current directory, ie. /blog. Without having to explicitly state the /blog directory.

  • You had an erroneous % at the end of the file?

I'd love to get anything that would normally be 404 to get redirected to the /blog part so content doesn't get lost on the migration.

This can perhaps be refined if we know the format of your original URLs, however, we basically need to redirect any request for anywhere outside of the /blog subdirectory - that would trigger a 404 - to be redirected to the /blog subdirectory.

You need to create a new .htaccess file in the document root with the following:

RewriteEngine On

# Redirect any 404s back to the "/blog" directory
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule !^blog/ /blog%{REQUEST_URI} [R=301,L]

The above would redirect a request for /foo to /blog/foo. But a request for /blog/bar would not be touched by this directive and routed through WordPress as normal (probably resulting in a 404 generated by WordPress).

You should first test with a 302 (temporary) redirect to avoid potential caching issues and only change to a 301 (permanent) redirect once you have confirmed it works as intended.

  • Related