This is How to Convert WordPress URL Query Strings to Slashes and Mask Single Paths Using .htaccess
One of the key aspects of creating a clean, user-friendly, and SEO-optimized website is the structure of your URLs. In WordPress, by default, URLs can sometimes look messy with query strings and parameters that don't offer the best user experience or search engine optimization.
Fortunately, WordPress provides us with ways to clean up and optimize these URLs. In this article, we'll explore two techniques:
- Converting WordPress URL query strings to slashes
- Masking single paths using the .htaccess file
Converting WordPress URL Query Strings to Slashes
WordPress, by default, often generates URLs that include query strings. For example, a post URL might look like this:
https://yoursite.com/?p=123
While this URL works, it's not the most user-friendly or SEO-optimized option. Ideally, we'd like to have a URL that looks more like this:
https://yoursite.com/my-post-title
To achieve this, we can use the template_include
filter in WordPress. This filter allows us to intercept the template loading process and make modifications as needed.
Here's how you can use the template_include
filter to convert query strings to slashes:
add_filter('template_include', 'convert_query_strings_to_slugs');
function convert_query_strings_to_slugs($template) {
global $wp_query;
// Check if the current request is a single post
if (is_singular()) {
// Get the post ID from the query string
$post_id = get_query_var('p');
// Get the post object
$post = get_post($post_id);
// Check if the post exists
if ($post) {
// Get the post's permalink
$permalink = get_permalink($post);
// Redirect to the permalink
wp_redirect($permalink, 301);
exit;
}
}
return $template;
}
Here's how this code works:
- We hook into the
template_include
filter.
- Inside the
convert_query_strings_to_slugs
function, we first check if the current request is for a single post using the is_singular()
function.
- If it is a single post, we retrieve the post ID from the query string using
get_query_var('p')
.
- We then get the post object using
get_post($post_id)
.
- If the post exists, we retrieve its permalink using
get_permalink($post)
.
- Finally, we redirect the user to the permalink using
wp_redirect($permalink, 301)
, which will convert the query string to a clean URL with slashes.
This approach ensures that all single post URLs are converted to their permalink format, which is better for user experience and SEO.
Masking Single Paths Using .htaccess
Another way to clean up your WordPress URLs is to mask single paths using the .htaccess
file. This can be useful if you have specific pages or post types that you want to display with a clean, simple URL.
For example, let's say you have a "Contact" page that you want to be accessible at https://yoursite.com/contact
instead of https://yoursite.com/?page_id=123
.
To achieve this, you can add the following code to your .htaccess
file:
# Mask single paths
RewriteRule ^contact/?$ index.php?page_id=123 [L]
This rule will intercept any requests to the /contact
URL and rewrite it to the appropriate page ID (?page_id=123
). The [L]
flag indicates that this is the last rule to be processed.
You can add similar rules for other single paths you want to mask. For example, if you have a "Blog" section that you want to be accessible at https://yoursite.com/blog
, you can add the following rule:
# Mask single paths
RewriteRule ^blog/?$ index.php?post_type=post [L]
This rule will rewrite the /blog
URL to the appropriate post type (?post_type=post
).
Keep in mind that when using this approach, you'll need to ensure that the underlying post, page, or custom post type actually exists in your WordPress database. Otherwise, you may encounter 404 errors.
Combining the Techniques
You can combine the techniques we've discussed to create a comprehensive URL optimization solution for your WordPress site. Here's an example of how you can do this:
add_filter('template_include', 'convert_query_strings_to_slugs');
function convert_query_strings_to_slugs($template) {
global $wp_query;
// Check if the current request is a single post
if (is_singular()) {
// Get the post ID from the query string
$post_id = get_query_var('p');
// Get the post object
$post = get_post($post_id);
// Check if the post exists
if ($post) {
// Get the post's permalink
$permalink = get_permalink($post);
// Redirect to the permalink
wp_redirect($permalink, 301);
exit;
}
}
// Check if the current request matches a masked path
$masked_paths = array(
'contact' => 123, // Page ID for the "Contact" page
'blog' => 'post', // Post type for the "Blog" section
);
foreach ($masked_paths as $path => $target) {
if (preg_match('#^' . $path . '/?$#', $wp_query->request)) {
$url = add_query_arg(is_numeric($target) ? array('page_id' => $target) : array('post_type' => $target), home_url());
wp_redirect($url, 301);
exit;
}
}
return $template;
}
In this example, we've combined the two techniques:
- We first use the
template_include
filter to convert query strings to permalinks for single posts.
- Then, we check if the current request matches any of the masked paths we've defined (in this case,
/contact
and /blog
). If it does, we redirect the user to the appropriate URL with the correct query parameters.
By combining these techniques, you can effectively clean up and optimize your WordPress URLs, providing a better user experience and improving your site's SEO.
Remember to test your changes thoroughly, as URL changes can have significant impacts on your site's performance and search engine visibility. It's also a good idea to keep a backup of your .htaccess
file in case you need to revert any changes.
If you're looking for a more comprehensive solution to optimize your WordPress website's performance, conversion rates, and user experience, consider checking out Flowpoint.ai. Flowpoint can help you identify all the technical errors that are impacting your conversion rates and directly generate recommendations to fix them.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.