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


I'm honestly struggling to comprehend this failure state - I believe I've reproduced the issue locally, and in my testing the RewriteRule directive hits but then WordPress processes the original input, resulting in a fairly catch-all attachment rewrite producing a query for a non-existent attachment. I may continue playing with it to try and figure out what's going on.

In the meantime, I think the root of the issue is complexity introduced by using a URI as the target of the RewriteRule. Using a file path seems to alleviate the problem:

RewriteRule ^page-check-([A-Z0-9_-]+)/highlights/?$ index.php?pagename=page-highlights&id=$1 [L]

Regarding the use of add_rewrite_rule(), I have a few notes:

  • When you use a target which does not start with index.php, WordPress will write the rule to the .htaccess file and implicitly begin the pattern with the ^ character - thus, starting a pattern with ^ while using a URI target can result in malformed rule beginning with ^^.
  • Instead of positional references to matches such as $1, WordPress uses the syntax $matches[1].
  • Flushing rewrite rules is a fairly expensive operation - I'd strongly recommend against flushing them in routine hook!

The above in mind, an add_rewrite_rule() with using a filepath target akin to the above RewriteRule directive would look as such:

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-highlights&id=$matches[1]', 'top');
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.