mobile site redirect with htaccess or php

Kracov

Member
Jan 26, 2006
52
0
66
kracov.org
I've been trying to use .htaccess for redirecting mobile users to the mobile version of my site. For two people, it doesn't seem to work.

mobile site: http://m.kracov.org

code inside .htaccess:

Code:
ErrorDocument 404 http://kracov.org/404.html

RewriteEngine On
# Check for mime types commonly accepted by mobile devices

RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC] RewriteCond %{REQUEST_URI} /$ RewriteRule ^  http://m.kracov.org%{REQUEST_URI} [R,L]


I also tried Mobile Detect- I searched around and someone said to put this code into the index file of my site:

Code:
<?php
@include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile() && isset($_COOKIE['mobile']))
{
$detect = "false";
}
elseif ($detect->isMobile())
{
header("Location:http://m.kracov.org");
}
?>

And also put the php file at the same location as the index file. But it just displays as text on my site. I'll admit I don't know what I'm doing.
 

watdaflip

Member
Feb 11, 2013
25
0
0
Does you index file have an extension of .php or .html?

Any PHP code will output as plain text unless the file extension is ".php" (assuming the server has PHP installed to parse .php files).

Assuming the extension was ".php", and you have PHP installed, try adding the following code:
Code:
error_reporting(E_ALL);
ini_set("display_errors", "on");

after:
Code:
<?php 
@include("Mobile_Detect.php");
In your current code.

This will ensure it outputs any PHP errors, so you can report back if that happens.

The only reasons I can see this not having redirected is either
1) PHP isn't installed, so even with a .php extension it will just display it as text
2) Somewhere in the file you include, it outputs some text, which will cause header() to fail since it has to be called before any output it sent..

Let me know and I'll see if I can help some more.