How can i redirect one url to another url using .htaccess or add_rewrite_rule
I am stuck at redirecting one url to another url.
I want to redirect
https://samedomain.com/page-check-927GSJAYS639AL/highlights
to
https://samedomain.com/page-highlights/
(927GSJAYS639AL this is id it can be anything)
the http://samedomain.com/page-highlights/
is the wordpress page
The rewrite rule I tied in .htaccess file (added this in the first line it-self) is:
RewriteEngine On
RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ page-highlights?id=$1 [L]
but I see a 404 error page
It worked for
RewriteEngine On
RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ info.php?id=$1 [L]
I also tried WP rewrite API php code
UPDATE 1
add_action('init', 'ao_add_rewrite_rule');
function ao_add_rewrite_rule() {
add_rewrite_rule('^page-check-([A-Z0-9_-]+)/highlights/?$', 'index.php?pagename=page-highlightsid=$matches[1]', 'top');
}
UPDATE 2
add_action('init', 'ao_add_rewrite_rule');
function ao_add_rewrite_rule() {
add_rewrite_rule('^page-check-([A-Z0-9_-]+)/highlights/?$', 'index.php?pagename=page-highlightsid=$matches[1]', 'top');
}
add_filter( 'query_vars', 'wpa5413_query_vars' );
function wpa5413_query_vars( $query_vars ) {
$query_vars[] = 'id';
return $query_vars;
}
Adding this line RewriteRule ^page-check-([A-Z0-9_-]+)/highlights$ index.php?pagename=page-highlightsid=$1 [L]
in .htaccess file works, but it will do 301 redirect, so the URL also changes back to https://samedomain.com/page-highlights/
No Luck :( in finding solutions
Any help is highly appreciated. Thanks in advance.
Topic apache rewrite-rules mod-rewrite htaccess Wordpress
Category Web