Search code examples
.htaccessurlmod-rewriteurl-rewritingamp-html

.htaccess redirect AMP from middle of URL to the end?


how can I by using rewritecond redirect all AMP urls like this (for all articles): domain.com/amp/some-article-url/ => domain.com/some-article-url/amp/ I have this code that doing opposite and I don't know how to reverse:

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

Solution

  • You may use this rule:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/+(.*/)?amp/(?!page/\d+)([^?\s]+?)/?[?\s] [NC]
    RewriteRule ^ /%1%2/amp/ [R=301,L,NE]
    

    Better to use THE_REQUEST instead of REQUEST_URI as REQUEST_URI may change due to execution of other rules.