Home › Forums › Back End › How to create wildcard redirects using wp_redirect › Reply To: How to create wildcard redirects using wp_redirect
November 8, 2015 at 6:37 am
#234696
Alen
Participant
@klmnweb I’ve tested following locally and it works. I didn’t have bbpress installed but I replicated the URL structure by creating sub pages. /forums/
, /forums/user
and /forums/user/jamie
add_action('init','redirect_edit_user_link');
function redirect_edit_user_link() {
if ( current_user_can('publish_posts') ) {
if ( strpos($_SERVER['REQUEST_URI'], '/forums/user/') !== false ) {
wp_redirect('/developers');
exit;
}
}
}
While above code works, I would still make the redirect at server level. Why bother processing PHP. Following will redirect ex: /forums/user/adam
to /developers/adam
.
# Apache
RedirectMatch 301 /forums/user/(.*) /developers/$1
# Nginx
location ~ /forums/user/(.*) {
rewrite ^(.*)$ /developers/$1 redirect;
}
Hope that helps, Alen.