.htaccess » How to move WordPress pages to the Domain Root

How to move WordPress pages to the Domain Root

I have a WordPress blog on pcelarstvopejcic.com/blog and a few pages that I would like to move to the domain document root, so that:

  • pcelarstvopejcic.com/blog/contact becomes pcelarstvopejcic.com/contact
  • pcelarstvopejcic.com/blog/shop becomes pcelarstvopejcic.com/shop

So this way I only have one WP installation and blog posts would be accessible via pcelarstvopejcic.com/blog/POST and pages in pcelarstvopejcic.com/PAGE

To do this, edit the functions.php file of your active theme and add:

add_filter('page_link', 'pages_in_root_page_link');
function pages_in_root_page_link($url) {
    $parsed_url = parse_url(get_home_url());
    return str_replace($parsed_url['path'] . '/', '/', $url);

then inside the .htaccess file change the default WP .htaccess file to:

# BEGIN WordPress

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shop/?$ /blog/about/ [L]
RewriteRule ^contact/?$ /blog/contact/ [L]
RewriteRule . /index.php [L]

# END WordPress

Where shop and contact are page names.

See also  5 Easy Ways To Speed Up Your WordPress ⚡

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.