This question may look like its been asked already and i have seen them all, i've been looking and attempting a lot of answers AND answers that weren't approved. I have successfully made it so that if the user goes to desktop version it will go to the mobile site and even if they go to places such as.

www.domain.com/aboutus

it would take them to

m.domain.com/?page=aboutus

So here is where the problem lies, not that it doesn't work, but I've been trying to remove the $_GET variable from the redirection the "?page=" part.

my .htaccess looks something like...

<IfModule mod_rewrite.c>

    RewriteEngine on

    # if it is not a file or folder, rewrite to index.php?page=<value>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

</IfModule>

<IfModule mod_rewrite.c>

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteRule ^index.php(.*)$ http://m.domain.com/ [QSA,L]

</IfModule>

I've tried adding the request filename with the redirection for mobile but to no avail. There are websites who have achieved it like 9gag by using the in built Google Chrome inspect element, google changes the user agent to devices that are selected (Mobile Phones) and I've used that to test how the redirection goes. so if i write 9gag.com/hot - it would take me to m.9gag.com/hot not m.9gag.com/?page=hot or wherever.

Thanks in advance, I've really been bothered by this.

1

There are 1 best solutions below

4
On BEST ANSWER

You need to check the mobile redirect first, and you need to include the request URI.

<IfModule mod_rewrite.c>

    RewriteEngine on

    # Redirect mobile requests to the mobile site
    # (but don't redirect when accessing certain directories)
    RewriteCond %{REQUEST_URI} !^/images [NC]
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
    RewriteRule ^(.*)$ http://m.domain.com/$1 [R=302,L]

    # If it is not a file or folder, rewrite to index.php?page=<value>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

</IfModule>

You can make the redirect permanent by changing 302 to 301.