After more research on url-rewriting add-rewrite-rule and rewrite-rules
I got introduced to Redirect Canonical
Redirects incoming links to the proper URL based on the site url.
Search engines consider www.somedomain.com and somedomain.com to be two different URLs when they both go to the same location. This SEO enhancement prevents penalty for duplicate content by redirecting all incoming links to one or the other.
And because of that all the paginated pages where been redirected to the domain.com/lorem=ipsum/photos when I wanted domain.com/lorem-ipsum/photos/page/2
Because of that I needed to change the redirect_canonical
with the filter redirect_canonical
PS: If you wrote a good rewrite rule, but it’s taking you to another page It can be redirect_canonical acting on your rewrite rule
I hope it helps anyone else, sorry for bad english.
You can check my final functions.php
add_action( 'after_setup_theme', 'rm_setup_theme' );
function rm_setup_theme() {
add_filter( 'rewrite_rules_array', 'rm_custom_rules' );
add_filter( 'query_vars', 'rm_custom_vars' );
add_filter( 'template_redirect', 'rm_custom_redirect' );
add_filter( 'redirect_canonical', 'rm_redirect_canonical', 10, 2 );
}
function rm_custom_rules( $rules ) {
$r = array(
'([^/]+)(/[0-9]+)?/photos/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&page=$matches[2]&photos=1&paged=$matches[3]',
'([^/]+)(/[0-9]+)?/photos/?$' => 'index.php?name=$matches[1]&page=$matches[2]&photos=1',
);
return array_merge( $r, $rules );
}
function rm_custom_vars( $qv ) {
array_push( $qv, 'photos' );
return $qv;
}
function rm_custom_redirect( ) {
global $wp_query;
$f = $wp_query->get( 'photos' );
if ( $f ) {
require TEMPLATEPATH . '/content-photos.php';
exit;
}
}
function rm_redirect_canonical( $redirect_url, $requested_url ) {
global $wp_rewrite;
// Abort if not using pretty permalinks, is a feed or is not a singular post page
if ( !$wp_rewrite->using_permalinks() || is_feed() || !is_singular( 'post' ) )
return $redirect_url;
// Get the original query parts
$redirect = @parse_url( $requested_url );
$original = $redirect_url;
if ( !isset( $redirect['query'] ) )
$redirect['query'] = '';
// If paged append pagination
if ( get_query_var( 'paged' ) > 0 ) {
$paged = (int) get_query_var( 'paged' );
$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
if ( $paged > 1 )
$redirect_url .= user_trailingslashit( "page/$paged", 'paged' );
}
if ( $redirect_url == $original )
return $original;
// tack on additional query vars
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
if ( $redirect_url && !empty($redirect['query']) ) {
parse_str( $redirect['query'], $_parsed_query );
$_parsed_query = array_map( 'rawurlencode', $_parsed_query );
$redirect_url = add_query_arg( $_parsed_query, $redirect_url );
}
return $redirect_url;
}
define( 'SITE_NAME', get_bloginfo( 'name' ) );
define( 'SITE_URL', home_url() );
define( 'THEME_URL', get_template_directory_uri() . "https://wordpress.stackexchange.com/" );