0

I use directive .htaccess

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} !(^|&)appId=
RewriteCond %{QUERY_STRING} !(^|&)q=
RewriteRule ^(.+?)\.html$ https://%{HTTP_HOST}/$1.html? [L,R=301]
</IfModule>

The problem would be solved if this RewriteCond %{QUERY_STRING} !^$ replace with this RewriteCond %{THE_REQUEST} \?

But this directive does not work on the Litespeed server. That's for sure, I contacted the developers of this server.

How to cut (redirect) to a link without a question mark via function.php?

example: if open website.com/post-name.html? >>>redirect>>> website.com/post-name.html

Sorry for my English.

4
  • 1. "I use directive .htaccess" - what does it really do? 2. "How to cut (redirect) to a link without a question mark" - you mean if the URL ends with just ?, i.e. no query string parameters specified? 3. What exactly are you trying to do? Why do you need to cut the ?? What does doing that solve?
    – Sally CJ
    Jan 4, 2022 at 3:42
  • Yes, that's right. The Yandex search engine takes somewhere and tries to index links with a question mark at the end. I want to fix this. Jan 4, 2022 at 6:36
  • So I've posted an answer, but have you tried this, i.e. something like User-agent: Yandex Disallow: /*?$ ? You may also find something helpful here.
    – Sally CJ
    Jan 4, 2022 at 13:29
  • 1
    The solution to the problem at the site level seems to me to be optimal. Jan 4, 2022 at 15:31

1 Answer 1

1

Try this, which uses the parse_request hook: (you can add other conditions, e.g. check whether $wp->query_vars['name'] is not empty, if you want to)

add_action( 'parse_request', 'wpse_400900_parse_request', 1 );
function wpse_400900_parse_request( $wp ) {
    // Redirect if the URL ends with a ? like https://example.com?
    if ( ! empty( $_SERVER['REQUEST_URI'] ) &&
        ltrim( $_SERVER['REQUEST_URI'], '/' ) === "{$wp->request}?"
    ) {
        wp_redirect( home_url( $wp->request ) );
        exit;
    }
}
1
  • 1
    Thank you. Exactly what is needed! Jan 4, 2022 at 15:29

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.