---
title: "htaccess hjälp för mobila enheter"
type: "forum-thread"
url: "https://www.webforum.nu/amne/webbservrar/189536-htaccess-hjälp-för-mobila-enheter"
topic: "Webbservrar"
topic_url: "https://www.webforum.nu/amne/webbservrar"
author: "dwmedia"
published: "2012-03-31T09:36:15.000Z"
updated: "2012-04-08T18:40:06.000Z"
replies: 6
views: 1147
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/webbservrar/189536-htaccess-hjälp-för-mobila-enheter"
---

# htaccess hjälp för mobila enheter

## #1 — dwmedia, 2012-03-31T09:36Z

Så här ser min kod ut, men om dem besöker typ 

<https://www.domain.com>

Så vill jag om dem besöker via mobilen att dem går till 

<http://m.domain.com> men det blir 
<https://m.domain.com> 

men om dem besöker den typ så här 
<http://domain.com> 
så kommer den automatiskt att skickas till 
<http://m.domain.com> 

Men om dem besöker standard hemsidan via SSL så hedrar den till mobil sidan med HTTPS också. 

Har inte hunnit fått mina SSL secikat för min mobila sida. 
Går det och skriva om den så det blir som den heder jag har på 2 raden längst ner i koden? 

\<IfModule mod_rewrite.c\>
    RewriteBase /
    RewriteEngine On

    \# Check if mobile=1 is set and set cookie 'mobile' equal to 1
    RewriteCond %{QUERY_STRING} (^\|&)mobile=1(&\|$)
    RewriteRule ^ - \[CO=mobile:1:%{HTTP_HOST}\]

    \# Check if mobile=0 is set and set cookie 'mobile' equal to 0
    RewriteCond %{QUERY_STRING} (^\|&)mobile=0(&\|$)
    RewriteRule ^ - \[CO=mobile:0:%{HTTP_HOST}\]

    \# cookie can't be set and read in the same request so check
    RewriteCond %{QUERY_STRING} (^\|&)mobile=0(&\|$)
    RewriteRule ^ - 

    \# Check if this looks like a mobile device
    RewriteCond %{HTTP:x-wap-profile} !^$ \[OR\]
    RewriteCond %{HTTP_USER_AGENT} "android\|blackberry\|ipad\|iphone\|ipod\|iemobile\|opera mobile\|palmos\|webos\|googlebot-mobile" \[NC,OR\]
    RewriteCond %{HTTP:Profile}       !^$

    \# Check if we're not already on the mobile site
    RewriteCond %{HTTP_HOST}          !^m\\.
    \# Check to make sure we haven't set the cookie before
    RewriteCond %{HTTP:Cookie}        !\\mobile=0(;\|$)
    \# Now redirect to the mobile site
    RewriteRule ^ http://m.domain.com%{REQUEST_URI} \[R,L\]
\</IfModule\>

Permalänk: https://www.webforum.nu/p/189536

## #2 — nitro2k01, 2012-03-31T10:05Z

Heder? Hedra? Secikat?

Hursomhelst, regeln redirectar ju till http://m.domain.com%{REQUEST_URI} så mobila användare borde hamna på icke-SSL-varianten. Är det möjligen så att m.domain.com pekar mot en helt annan webbrot med en annan .htaccess som rikat om besökaren till https?

Kan man få se sidan live online?

Permalänk: https://www.webforum.nu/p/2297162

## #3 — dwmedia, 2012-04-01T02:42Z

Ser inte ut som man får till det så här ser hela htaccessen ut. 

\<ifModule\>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\[a-zA-Z0-9\_-\]+)/?$ index.php?r=$1 \[L,QSA\] 
RewriteRule ^(\[a-zA-Z0-9\_-\]+)/(\[a-zA-Z0-9\_-\]+)/?$ index.php?r=$1&lang=$2 \[L,QSA\] 
RewriteRule ^(\[a-zA-Z0-9\_-\]+)/(\[a-zA-Z0-9\_-\]+)/(\[a-zA-Z0-9\_-\]+)/?$ index.php?r=$1&lang=$2&epi=$3 \[L,QSA\] 
\</ifModule\>

\<IfModule mod_rewrite.c\>
    RewriteBase /
    RewriteEngine On

    \# Check if mobile=1 is set and set cookie 'mobile' equal to 1
    RewriteCond %{QUERY_STRING} (^\|&)mobile=1(&\|$)
    RewriteRule ^ - \[CO=mobile:1:%{HTTP_HOST}\]

    \# Check if mobile=0 is set and set cookie 'mobile' equal to 0
    RewriteCond %{QUERY_STRING} (^\|&)mobile=0(&\|$)
    RewriteRule ^ - \[CO=mobile:0:%{HTTP_HOST}\]

    \# cookie can't be set and read in the same request so check
    RewriteCond %{QUERY_STRING} (^\|&)mobile=0(&\|$)
    RewriteRule ^ - 

    \# Check if this looks like a mobile device
    RewriteCond %{HTTP:x-wap-profile} !^$ \[OR\]
    RewriteCond %{HTTP_USER_AGENT} "android\|blackberry\|ipad\|iphone\|ipod\|iemobile\|opera mobile\|palmos\|webos\|googlebot-mobile" \[NC,OR\]
    RewriteCond %{HTTP:Profile}       !^$

    \# Check if we're not already on the mobile site
    RewriteCond %{HTTP_HOST}          !^m\\.
    \# Check to make sure we haven't set the cookie before
    RewriteCond %{HTTP:Cookie}        !\\mobile=0(;\|$)
    \# Now redirect to the mobile site
    RewriteRule ^ http://m.domain.com%{REQUEST_URI} \[R,L\]
\</IfModule\>

Permalänk: https://www.webforum.nu/p/2297172

## #4 — dwmedia, 2012-04-05T18:21Z

Bump hoppas någon kan lösa det får fan inte till det :/

Permalänk: https://www.webforum.nu/p/2297316

## #5 — Danne V, 2012-04-05T20:29Z

> **[dwmedia skrev:](https://www.webforum.nu/p/1549315)**
>
> Bump hoppas någon kan lösa det får fan inte till det :/

Alltså....

För det första skriver du tyvärr så enbarmligt dålig svenska och stavar så åt helvete att man måste gissa vad det är du menar. Man orkar helt enkelt inte att läsa. Kolla gärna <http://www.backendmedia.se/2006/08/10/ska-jag-skriva-dom-de-eller-dem/>. Bokmärk och försök komma ihåg.

För det andra så funkar inte browser- eller systemsniffing särskilt bra över huvud taget, och i synnerhet inte när det gäller handhelds (som t.ex. mobiltelefoner).

Det du kan göra är att erbjuda länkar från ena varianten till den andra och eventuellt länka till <https://www.handsetdetection.com/> som kan hjälpa en del.

Permalänk: https://www.webforum.nu/p/2297320

## #6 — dwmedia, 2012-04-08T18:01Z

Sorry :)

Men även om jag kör med google mobile detect 

include("\_scripts/mobil/Mobile_Detect.php");
$detect = new Mobile_Detect();

if ($detect-\>isMobile()) {

   header("Location: http://m.domain.com");
   exit;
}

Så besöker dem 
<http://domain.com> så går dem till <http://m.domain.com> 

Går dem via 
<https://www.domain.com> / <http://www.domain.com> / <https://www.domain.com> 

så går dem automaatiskt till 
<https://m.domain.com> 

Så det spelar som ingen roll om jag skriver i en class eller om jag skriver i htaccess går den via https adress så går dem till https adress på subdomänen. 

Blir snart så förbannad att jag köper mig en SSL för mobila sidan också för få det löst.

Permalänk: https://www.webforum.nu/p/2297376

## #7 — dwmedia, 2012-04-08T18:40Z

Löste det med javascript header en vanlig header fungerar inte. 

include("\_scripts/mobil/Mobile_Detect.php");
$detect = new Mobile_Detect();

if ($detect-\>isMobile()) {
   echo "\<script type='text/javascript'\>self.location=('http://m.domain.com')\</script\>";
}

Permalänk: https://www.webforum.nu/p/2297378

---

Tråden på webben: https://www.webforum.nu/amne/webbservrar/189536-htaccess-hjälp-för-mobila-enheter
