我正在使用 wordpress 创建一个求职网站。我想创建一个自定义的 .htaccess 规则,这样当用户在表单中搜索工作时,第一部分将作为文件夹结构,其余部分将作为查询参数添加。
这将仅当我们查询参数 ?find= any-value 位于 url 其余页面想要的时候,就像http://example.com/contact/
我想要 :-
http://example.com/?find=any-value&job_location=Austin,TX&findjobs=Search
成为 :-
http://example.com/any-value?job_location=Austin,TX&findjobs=Search
我的永久链接设置就像:-
http://example.com/sample-post/
质量检查 .htaccess 代码
# Special redirects
RewriteCond %{QUERY_STRING} ^find=([^/]*)$
RewriteRule ^/?$ /%1\/? [R=301,L]
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
您应该wp_redirect
在template_redirect
钩子中使用函数。
可以加在 functions.php
例子:
function my_redirect_function() {
// check if argument is set
if ( $_GET['find'] ) {
// get site url
$url = get_site_url() . '/' . $_GET['find'];
// clone GET arguments
$query = $_GET;
// remove the find argument
unset( $query['find'] );
// add all get arguments to the new url
$url = add_query_arg( $query, $url );
// redirect to the generated url
wp_redirect( $url );
exit();
}
}
add_action( 'template_redirect', 'my_redirect_function' );
链接:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句