Trang chủ
Cảm ơn 2000 khách hàng thiết kế website - Thiết kế web siêu rẻ 1,5 tr - Hosting đặt tại fpt không hạn chế băng thông, dung lượng 500k - năm - Tên miền .com .net giá rẻ nhất Việt Nam: 180k - năm - Bảng giá thiết kế web rẻ nhất việt nam 1,5 tr -
DANH MỤC SẢN PHẨM

WEB GIÁ RẺ 1,5 TRIỆU

Thiết kế web giá rẻ THANHBINHAUTO.COM
Thiết kế web giá rẻ MÁY NÉN KHÍ BẢO TÍN
Thiết kế web giá rẻ FORCI.VN
Thiết kế website giá rẻ QUATETST.COM
Thiết kế web giá rẻ NƯỚC MẮM VỊ THANH
Thiết kế web giá rẻ xkldnhatban.vn
Thiết kế website bán đồ gỗ DOGONGOCBICH.VN
Thiết kế website giá rẻ NESVI.COM.VN
Thiết kế web giá rẻ SLC.COM.VN
Thiết kế web giá rẻ thietbikhachsan.biz
Thiết kế web giá rẻ FUJISFOODS.COM
Thiết kế web giá rẻ HLCO.COM.VN
Thiết kế web giá rẻ SONNANOVN.VN
Thiết kế website giá rẻ zuhyorganic.com
Thiết kế website GREENMED.COM
Thiết kế web giá rẻ FDILAW
Thiết kế web giá rẻ DANLOIDAMME.VN
Thiết kế web giá rẻ KENHNHADATCHINHCHU
Thiết kế web giá rẻ GẠCH VIGLACERA VIỆT THẮNG
Thiết kế web giá rẻ cho thuê xe
Thiết kế web giá rẻ KTSHANOI.NET
Thiết kế web giá rẻ CHOTHIETBIVN.COM
Thiết kế web giá rẻ công ty TNHH TM và DV chiếu sáng Đức Anh
Thiết kế website VESPASIDECARCLUB.COM
Thiết kế web giá rẻ WINCOMHANOI.COM
Thiết kế web giá rẻ OSSAN.VN
Thiết kế web giá rẻ MAYVANPHONGSIEURE.COM
Thiết kế web giá rẻ Lioa Nhật Linh
Thiết kế web giá rẻ linh kiện cầu thang
Thiết kế web giá rẻ MUACHUNG365.NET

tất cả về htaccess

Redirect to a Mobile Site with .htaccess and Set a Cookie to Break Redirect

Cập nhật: 7/18/2015 - Số lượt đọc: 14054
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Xem slide ảnh gốc

Mục lục [ Ẩn / Hiện ]

Recently mobile websites have become extremely popular and important in the web development world. The days of people having crappy flip phones with no internet or javascript are over. People have quality phones now, so having a mobile optimized website is no longer a luxury but a necessity.

If you’re looking into developing a mobile website for your business or for your client make sure to checkout my mobile website and theme pack “emdot”, available exclusively on ThemeForest. For only $9 you get a fully functioning mobile site with 12 different themes, a validating contact form, a portfolio with mobile touch, an “add to home bubble” for iPhones and iPads, 50+ icons for the navigation, 20 social media icons, and a bunch more. Simply pop in your content and you’re ready to go. Well almost ready to go, the last thing you’re going to need is to redirect people to your new mobile website.

Redirecting people to your mobile website is easy enough by using a .htaccess file. Basically you say “if people visiting my website are on a mobile device send them to http://m.example.com, and if they aren’t on a mobile device send them to http://example.com”. This of course assumes that you setup a subdomain located at “m.example.com”.

After setting this up for a client I realized one problem with this. When setting up your mobile website you should have a link in the footer allowing the user to visit your full website (you most likely have some content on your full website that you don’t have on your mobile website). Your link would look something like this:

<a href="http://example.com">Visit full site</a>

Easy enough, except for one thing.. we redirected the users from the main site to the mobile site. So if they click the “visit full site” link they will be redirected back to the mobile site again.

The solution is to set a cookie which allows you to visit the full site without being redirected. Setting the cookie allows you to go from the mobile site to the full site, and visit any page on the full site once you’re there without being redirected back to the mobile site.

Step 1.

Setup your mobile site at http://m.example.com

Step 2.

Add the following link to your mobile site:

<a href="http://www.example.com?m=0">Full Website</a>

*If you’re using jQuery mobile (such as the emdot theme), you may need to add rel=”external” to your link, like this:

<a rel="external" href="http://www.example.com?m=0">Full Website</a>

Step 3.

Create a .htaccess file (which is just a regular file such as a .txt files renamed .htaccess). Add the following code and replace the 2 domain names. Upload the file to the root of your website. If you already have a .htaccess file simply add this code to the existing file.

*Notes: Make sure you don’t overwrite your existing .htaccess file by accident. This only works on Apache servers.

RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.example.com]

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://m.example.com [R,L]

Credit. I tried a few different scripts to find a solution that worked for me, and the above script was the winner. The original script came from this thread on Stack Overflow.

No iPad

The following appears to work if you want a redirect but don’t want the iPad to be redirected. Code from the link above.

RewriteEngine on
RewriteBase /

# 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 ^ - [S=1]

    # Check if this looks like a mobile device
    RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|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.example.com%{REQUEST_URI} [R,L]

The code for the link to the full site with this technique:

<a href="http://www.example.com?mobile=0">Full Website</a>

Tags
Redirect to a Mobile Site with -htaccess and Set a Cookie to Break Redirect
Hãy gọi cho chúng tôi 0989383638
Hãy chát với chúng tôi fb/haanhco
Bạn có thể chát trực tuyến không cần nick, không cần fb, sky, zalo, vb góc dưới bên phải màn hình, chúng tôi luôn sẵn sàng hỗ trợ bạn.
CÁC TIN KHÁC
Tất cả .htaccess
Tất cả .htaccess
htaccess chuẩn của mptmed
htaccess chuẩn của mptmed
Code chuyen sang ban di dong php
Code chuyen sang ban di dong php
Xem thêm...
HỖ TRỢ TRỰC TUYẾN

Thời gian làm việc:
Sáng từ 8h => 12h
Chiều từ 14h => 17h 

Nghỉ chiều thứ 7 + ngày CN 


Hotline: 0989383638

Tư vấn thiết kế web

Hướng dẫn sử dụng

TƯ VẤN THIẾT KẾ WEB

WEB GIÁ RẺ 1,5 TRIỆU

Thiết kế web giá rẻ KINGSTONE.COM.VN
Thiết kế web giá rẻ nhà hàng
Thiết kế web giá rẻ MYNGHENGOCBICH.COM
Thiết kế web giá rẻ QUANGCAOTAMLON
Thiết kế web giá rẻ LEDANHKHANG.COM
Thiết kế web giá rẻ Gia Lợi
Thiết kế web giá rẻ GIAYWIN75.COM
Thiết kế web giá rẻ YDCPLUS.EDU.VN
Thiết kế web giá rẻ gdtxtinh.haiduong.edu.vn
Thiết kế web giá rẻ GUONGDEP.COM.VN
Thiết kế website giá rẻ tnc-electric.com
Thiết kế web giá rẻ vườn sinh thái
Thiết kế web giá rẻ Phú Hòa An
Thiết kế web giá rẻ CƠ ĐIỆN LẠNH MIỀN BẮC
Thiết kế web giá rẻ công ty công ty cung cấp ngoại thất hồ bơi Gia Phong
Thiết kế web giá rẻ cokhituangiang.com
Thiết kế web giá rẻ MÁY XÂY DỰNG 24H
Thiết kế web giá rẻ VINCAR.NET
Thiết kế website giá rẻ nptsmart.vn
Thiết kế website: KINHCHONGCHAY.VN
Thiết kế website giá rẻ CAOSU368.
Thiết kế website: apmttsvietnam.com
Thiết kế web giá rẻ trung tâm huấn luyện Tennis tennisvinhphuc.com
Thiết kế web giá rẻ CÔNG CHỨNG NGUYỄN ANH TUẤN
Thiết kế web giá rẻ trung tâm huấn luyện Tennis I Việt Nam
Thiết kế web giá rẻ thiết bị khoa học và y tế MPT
Thiết kế web giá rẻ Công ty TNHH TM và DV Long Dương Việt Nam
Thiết kế web giá rẻ Royal Hotel Nghi Tàm
Thiết kế website TRƯỜNG CAO ĐẲNG CÔNG NGHỆ Y - DƯỢC VIỆT NAM
Thiết kế website giá rẻ photohome.com.vn
THỐNG KÊ

Lượt truy cập: 11779536
Đang online: 3
Trang chủ  Giới thiệu Khách hàng Báo giá Dịch vụ Thanh toán Hỗ trợ Bản đồ Liên hệ

CÔNG TY THƯƠNG MẠI ĐIỆN TỬ HÀ ANH
Địa chỉ: Phòng 203, Tòa nhà CT7A, Khu ĐTM The Spark Dương Nội, Quận Hà Đông, Hà Nội.
Mobile: 0989 383638
Email: webgiare.haanhco@gmail.com 
Website:  https://webgiare.vn - https://webgiare.com.vn - https://webgiare.com

© 2005 - 2025. Toàn bộ bản quyền thuộc CÔNG TY THƯƠNG MẠI ĐIỆN TỬ HÀ ANH.

Hotline Facebook Chat facebook Zalo