我想将WordPress默认搜索页面(search.php
)重定向到/videos/search_string/
如果有分页内容,那么我想要这个网址/videos/search_string/page_no
默认搜索网址类似于?s=search_string
我已经使用下面的代码进行测试,但它无法正常工作,还尝试了更多。我现在要做什么来解决我的问题?我正在使用WordPress版本4.6
add_rewrite_rule( 'videos/([^/]+)/?$', 'index.php?s=$matches[1]', 'top' );
add_rewrite_rule( 'videos/([^/]+)(/videos/(\d+))/?$', 'index.php?s=$matches[1]&paged=$matches[3]', 'top' );
// redirect immediately
if ( isset( $_GET[ 's' ] ) ) {
$location = '/videos/' . $_GET[ 's' ];
if ( isset( $_GET[ 'paged' ] ) && $_GET[ 'paged' ] )
$location .= '/videos/' . $_GET[ 'paged' ];
wp_redirect( trailingslashit( home_url( $location ) ), 301 );
exit;
}
答案 0 :(得分:0)
您可以使用已有的正则表达式在apache Webserver上向.htaccess
添加重写规则:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^videos/([^/]+)/$ index.php?s=$1 [L]
RewriteRule ^videos/([^/]+)/([0-9]+)/$ index.php?s=$1&paged=$2 [L]
...或者使用你的PHP代码 - 如果你精确地编写它们都可以:
if ( isset( $_GET[ 's' ] ) && strlen( $_GET[ 's' ] ) ) {
$location = '/videos/' . urlencode( $_GET[ 's' ] );
if ( isset( $_GET[ 'paged' ] ) && $_GET[ 'paged' ] ) {
$location .= '/' . $_GET[ 'paged' ];
}
wp_redirect( trailingslashit( home_url( $location ) ), 301 );
exit;
}