2

I have to redirect mobile users to a new URL (/amp/...) so I added this snippet code to my Apache site config:

RewriteCond %{HTTP_REFERER} !^http://test.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/amp/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /amp/$1 [L,R=302]`

The problem is that I'm only redirected when requesting the home page (test.domain.com), and not when I request to any other URL (e.g. test.domain.com/category/web). Any idea of what I'm doing wrong?

This is a Wordpress site, and the full rewrite module is:

<Directory /var/www/project>
    DirectoryIndex index.php
    Options None FollowSymLinks
    AllowOverride none
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^uploads/(.+) http://myCDN.com/uploads/$1  [R,L]
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        RewriteCond %{HTTP_REFERER} !^http://test.domain.com [NC]
        RewriteCond %{REQUEST_URI} !^/amp/.*$
        RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
        RewriteRule ^(.*)$ /amp/$1 [L,R=302]
    </IfModule>
</Directory>
  • Aside: "Options None FollowSymLinks" - That doesn't really make sense, it should simply be Options FollowSymLinks. – MrWhite Apr 5 '17 at 7:48
1

Your redirect needs to go before the front controller (internal rewrite). The front controller is catching every request, except for the document root (home page). So your redirect is only being processed for the home page and is simply being ignored for all other requests.

In other words:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_REFERER} !^http://test.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/amp/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /amp/$1 [L,R=302]

RewriteRule ^uploads/(.+) http://myCDN.com/uploads/$1  [R,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

As a general rule, external redirects should always go before internal rewrites.

You mention this is in your "Apache site config". I assume these directives are in a <Directory> container within your "site config" (ie. in a directory context). (These directives would simply not work in a server config or virtual host context, ie. directly in your "site config".)

| improve this answer | |
  • I'm redirected to domain.com/amp/index.php when I request any url – Manolo Apr 4 '17 at 20:25
  • Ah, your front controller will still try to route the redirected URL. You'll need an exception to prevent this. I've updated my answer. – MrWhite Apr 4 '17 at 20:35
  • Umm, I'm not uing .htaccess and don't want to. I'm adding all rules in the /etc/apache2/sites-available/domain.conf. – Manolo Apr 5 '17 at 5:45
  • Yes, that's OK. But presumably these directives are inside a <Directory> container? It's just that a pattern such as ^uploads/(.+) would never match in a virtual host (or server config) context. Did you try the exception I added (update#2)? – MrWhite Apr 5 '17 at 7:02
  • Yes, inside a <Directory>. I tried, but this condition is checked at RewriteCond %{REQUEST_URI} !^/amp/.*$. Anyway, I have had to add it bellow the other rules in order to work, and I'm facing the same problem described in the question. – Manolo Apr 5 '17 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.