0

I create a new page with post name is 'my-page'. I also have 2 custom query strings. So, the URL will be like:

http://domain.com/my-page/?qst1=aaa&qst2=bbb

I want the URL like:

http://domain.com/aaa/bbb

How I can do it? How I can tell WP it is a valid URL, because WP will point to 404 page (aaa page not found).

3 Answers 3

1

In your .htaccess you should be able to do something like what is shown by Chris Coyier on CSS Tricks.

Link to example by Chris Coyier: https://css-tricks.com/snippets/htaccess/subdirectories-redirect-query-string/

Since you don't have .php in your URL, WordPress has most likely already created RewriteEngine rules for you to not need .php. But the result should still work fine.

EDIT:

Another example of this is shown with pretty much the same exact scenario as you. It's on Apache's Wiki website: https://wiki.apache.org/httpd/RewriteQueryString The example shown to do this in your .htaccess is something like this:

RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/path /path/%1/%2?
1

You can add add_rewrite_rule function in your website. For your case you can try this following code

add_action( 'init', 'wpse12065_init' );
function wpse12065_init() { 
add_rewrite_rule(
    'my-page(/([^/]+))?(/([^/]+))?/?',
    'index.php?pagename=my-page&qst1=$matches[2]&qst2=$matches[4]',
     'top'      
);

Then save your permalink.

In my-page Page ( you need to create custom page template ) you can catch that query vars with this code

if (isset($wp_query->query_vars['qst1']) && !empty($wp_query->query_vars['qst1']) ) {
$qst1 = $wp_query->query_vars['qst1'];
}
else {
//do something else 
}
//do the same step for qst2

Hope it helps

0

You can use permalink setting Custom Structure /%category%/%postname%/.

or You can use https://wordpress.org/plugins/simple-301-redirects/ plugin.

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.