最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to cut off question mark in url with functions.php

programmeradmin3浏览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/post-name.html? >>>redirect>>> website/post-name.html

Sorry for my English.

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/post-name.html? >>>redirect>>> website/post-name.html

Sorry for my English.

Share Improve this question asked Jan 3, 2022 at 21:19 UserUser-nameUserUser-name 577 bronze badges 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 Commented 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. – UserUser-name Commented 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 Commented Jan 4, 2022 at 13:29
  • 1 The solution to the problem at the site level seems to me to be optimal. – UserUser-name Commented Jan 4, 2022 at 15:31
Add a comment  | 

1 Answer 1

Reset to default 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?
    if ( ! empty( $_SERVER['REQUEST_URI'] ) &&
        ltrim( $_SERVER['REQUEST_URI'], '/' ) === "{$wp->request}?"
    ) {
        wp_redirect( home_url( $wp->request ) );
        exit;
    }
}
发布评论

评论列表(0)

  1. 暂无评论