Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active December 11, 2023 21:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/b7eb9cc7648b9a8b94b0015c79c8702e to your computer and use it in GitHub Desktop.
Save westonruter/b7eb9cc7648b9a8b94b0015c79c8702e to your computer and use it in GitHub Desktop.
<?php
/**
* AMP Rewrite Endpoint Removal plugin.
*
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2019 Google Inc.
*
* @package AMP_Rewrite_Endpoint_Removal
*
* Plugin Name: AMP Rewrite Endpoint Removal
* Description: Automatically redirect URLs with the /amp/ endpoint or the ?amp query var to the URL without. This is only useful when removing the AMP plugin. The plugin does nothing if the AMP plugin is active. Flush your permalinks if you experience problems.
* Version: 0.1
* Plugin URI: https://gist.github.com/westonruter/b7eb9cc7648b9a8b94b0015c79c8702e
* Author Name: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Gist Plugin URI: https://gist.github.com/westonruter/b7eb9cc7648b9a8b94b0015c79c8702e
*/
namespace AMP_Rewrite_Endpoint_Removal;
const SLUG = 'amp';
function init() {
if ( function_exists( 'amp_init' ) ) {
return;
}
add_amp_endpoint();
add_action( 'template_redirect', __NAMESPACE__ . '\redirect_non_amp' );
}
function add_amp_endpoint() {
add_rewrite_endpoint( SLUG, \EP_PERMALINK );
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\flush_rewrite_rules' );
add_action( 'init', __NAMESPACE__ . '\init' );
function flush_rewrite_rules() {
add_amp_endpoint();
\flush_rewrite_rules( false );
}
function redirect_non_amp() {
if ( false === get_query_var( SLUG, false ) && ! isset( $_GET[ SLUG ] ) ) {
return;
}
$original_url = wp_unslash( $_SERVER['REQUEST_URI'] );
$url = $original_url;
// Strip endpoint.
$url = preg_replace( ':/' . preg_quote( SLUG, ':' ) . '(?=/?(\?|#|$)):', '', $url );
// Strip query var.
$url = remove_query_arg( SLUG, $url );
if ( $url !== $original_url ) {
wp_safe_redirect( $url, 301 );
exit;
}
}
@westonruter
Copy link
Author

@westonruter
Copy link
Author

Note that instead of:

const SLUG = 'amp';

This plugin should be reusing the same logic in amp_get_slug() to account for custom AMP slugs. Namely:

function get_amp_slug() {
    return apply_filters( 'amp_query_var', defined( 'AMP_QUERY_VAR' ) ? AMP_QUERY_VAR : 'amp' );
}

@p2rvez
Copy link

p2rvez commented Mar 1, 2023

my amp url paired wit this: ?amp=1 what should i do?
i have used this regx redirection method:

^/(.*?)/\?amp=1.*
to
/$1/
it work. but do you still prefer this plugin or i am good with this code

@westonruter
Copy link
Author

@p2rvez This plugin checks if the URL contains a query var, and if so, it is removed and the user is redirected.

@Chris-wolf-bob
Copy link

Thanks. This worked. However, I was wondering if you had advice for "Duplicate without user-selected canonical" that Google search console shows for other page endings.
For example, Google console is showing these types of endings on my pages, but I can't figure a way to modify them:

/page/53/?tve=true&tcbf=4442db2c60
and
.com/?elementor_library=default-kit
and
/page/2/?fbclid=IwAR0IZbWC-Aus1OlJlyViBrgj4QRFRph4ymlXY7LFW8zg_lPbhxguuFcFoXA
and
/page/52/?cat=-1

@westonruter
Copy link
Author

@Chris-wolf-bob These URLs don't get a canonical link in WordPress. You might consider adding Yoast SEO which adds canonical links for non-singular URLs, like archives: https://yoast.com/features/canonical-url-tags/

@Chris-wolf-bob
Copy link

Chris-wolf-bob commented Nov 2, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment