Category Archives: amp-html

Apache rule to minimize redirects

I have the following in apache host file to redirect from www to non-www version of the site:

RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

I also want to redirect anyone going to this url:

www.domain.com/page/amp => domain.com/page/

The problem is that if I add the following rule to my .htaccess, it results in an extra redirect:

RewriteCond %{REQUEST_URI} (.+)/amp(.*)$ [NC]
RewriteRule ^ %1/ [R=301,L]

What happens is that when anyone goes to www.domain.com/page/amp it does two redirects:

First: www.domain.com/page/amp => 301 to => domain.com/page/amp

Second: domain.com/page/amp => 301 to => domain.com/page

How can I modify the rules in the vhosts file to minimize this extra redirect?

Example urls of what should ideally happen:

www.domain.com => domain.com
www.domain.com/page => domain.com/page
www.domain.com/page/amp => domain.com/page

That last one above is what I want to minimize a redirect for.

Update:

Here are both the vhosts (for port 80 and 443):

<VirtualHost *:80>
  DocumentRoot /var/www/html
  RewriteEngine on
  RewriteCond %{SERVER_NAME} =domain.com [OR]
  RewriteCond %{SERVER_NAME} =www.domain.com
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot /var/www/html
  ServerName domain.com
  Include /etc/letsencrypt/options-ssl-apache.conf
  ServerAlias www.domain.com
  SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>

How to redirect /amp URLs to the non AMP version?

Firstly, I would like to tell you guys that I have already searched the following threads for solutions, but they don't work.

.htaccess redirect /amp/ urls to nonamp version

How to redirect amp content to normal mode

Redirect from AMP URLs to Real URLs

I have added the rule: RewriteRule ^(.*)/amp$ $1 [R=301,L] in my .htaccess like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/amp$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

But it still doesn't take my amp links to their non-AMP original links. Please help me with this.

.htaccess rewriting URL to add a character to a URL

I'm doing this to fix an error with my AMP pages. I'm using the Automatic AMP plugin, and this plugin lets you access the AMP pages using 2 different methods

  1. site.com/post/amp/
  2. site.com/post/?amp

Using the AMP Page validator (https://validator.ampproject.org) I see that all AMP pages using just /amp/ get multiple errors, while /?amp is validated correctly.

Unfortunately, Google is checking /amp/ for all my pages, hence I'm getting tons of errors.

What I'd like to know is how to use the .htaccess redirect rule to add the ? to the AMP queries so all /amp/ requests are redirected (with a 301) to /?amp/

I'd appreciate suggestions on this. Thank you