重定向:自动重定向到我的网站

时间:2019-04-08 11:38:48

标签: php .htaccess redirect

我需要帮助来进行自动重定向。我需要使用www,没有www和语言检测功能的移动设备自动重定向到https:

1.https和www 2.在移动设备上连接到https://m.example.com/ 3.自动检测https://www.example.com/en/或/ el /或/ ru /移动设备https://m.example.com/en/或/ el /或/ ru /

的语言

这是我的.htaccess文件

Options -Indexes
AddDefaultCharset utf-8

ErrorDocument 402 /404.php
ErrorDocument 403 /404.php
ErrorDocument 404 /404.php
ErrorDocument 500 /404.php
ErrorDocument 501 /404.php
ErrorDocument 502 /404.php

DirectoryIndex index.php

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 

RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

#RewriteCond %{HTTP:Accept-Language} ^en [NC]
#RewriteRule ^$ /[L,R=301]

#RewriteCond %{HTTP:Accept-Language} ^el [NC]
#RewriteRule ^$ /el/ [L,R=301]

#RewriteCond %{HTTP:Accept-Language} ^ru [NC]
#RewriteRule ^$ /ru/ [L,R=301]

RewriteBase /

RewriteRule ^/$ index.php [L,QSA]
RewriteRule ^ru/(.*)$ /$1?lang=ru [L,QSA]
RewriteRule ^en/(.*)$ /$1?lang=en [L,QSA]
RewriteRule ^el/(.*)$ /$1?lang=el [L,QSA]
RewriteRule ^ajax/(.*)$ /$1?mode=ajax [L,QSA]
RewriteRule ^robots.txt$ misc.php?controller=robots [L,QSA]
RewriteRule ^image/([0-9]+)x([0-9]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).jpg$ resize.php?apid=$3&key=$4&w=$1&h=$2 [L,QSA]
RewriteRule ^attachment/([a-zA-Z0-9]+)/([0-9]+)/(.*)$ attachment.php?dmuid=$1&order=$2&original=$3 [L,QSA]
RewriteRule ^item/print/([a-zA-Z0-9_-]+)_([0-9]+)$ item.php?mode=print&controller=item&id=$2 [L,QSA]
RewriteRule ^item/([a-zA-Z0-9_-]+)_([0-9]+)$ item.php?controller=item&id=$2 [L,QSA]
RewriteRule ^moderation/list/(waiting|inactive|moderated)/$ moderation_list.php?controller=$1 [L,QSA]
RewriteRule ^my/(waiting|inactive|moderated)/$ my.php?controller=$1 [L,QSA]
RewriteRule ^my/message/([0-9]+)$ my_messages.php?controller=message&mid=$1 [L,QSA]
RewriteRule ^my/messages/([0-9]+)$ my_messages.php?controller=messages&aid=$1 [L,QSA]
RewriteRule ^my/messages/(sent|archive)/$ my_messages.php?controller=$1 [L,QSA]
RewriteRule ^pay/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ gateway.php?controller=gateway&code=$1&action=$2 [L,QSA]
RewriteRule ^pay/([a-zA-Z0-9_-]+)/$ pay.php?controller=$1 [L,QSA]
RewriteRule ^s-login/([a-zA-Z0-9_-]+)/$ /s-login.php?provider=$1 [L,QSA]
RewriteRule ^list/$ list.php?controller=list&query=list [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ list.php?controller=list&query=$1/$2/$3/$4 [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ list.php?controller=list&query=$1/$2/$3 [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ list.php?controller=list&query=$1/$2 [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ list.php?controller=list&query=$1 [L,QSA]

那是我的php功能:

 $config['siteurl'] = !empty($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'';
$config['mobileurl'] = 'example.com';

$config['langs'] = array('en', 'el', 'ru',);
$config['lang_names'] = array('en' => '<span class="flagen">EN</span>', 'el' => '<span class="flagel" >EL</span>', 'ru' => '<span class="flagru" >RU</span>');
if(isset($_GET['lang']) && !empty($_GET['lang'])) {
    if (!in_array(trim($_GET['lang']), $config['langs'])) {
        $config['lang'] = reset($config['langs']);
    } else {
        $config['lang'] = trim($_GET['lang']);
    }
}
else {
    $config['lang'] = $config['langs'][0];
}

unset($_GET['lang']);
if ($config['lang'] != reset($config['langs'])) {
    $langPrefix = '/' . $config['lang'];
} else {
    $langPrefix = '';
}

include "includes/mobiledetect/Mobile_Detect.php";
if (isset($_GET['force_mobile']) && trim($_GET['force_mobile']) == '0' && !empty($_GET['force_mobile'])) {
    setcookie('force_mobile', '0', $time + 60 * 60 * 24 * 365, '/', '.' . $config['siteurl']);
    header('Location: https://' . $config['siteurl'] . trim($_GET['ref']));
    exit;
}
$mdetect = new Mobile_Detect;
if ($mdetect->isMobile() && !$mdetect->isTablet()) {
    if (!$m) {
        if (!isset($_COOKIE['force_mobile']) || trim($_COOKIE['force_mobile']) == '1') {
            setcookie('force_mobile', '1', $time + 60 * 60 * 24 * 365, '/', '.' . $config['siteurl']);
            //header('Location: https://m.' . $config['siteurl'] . $langPrefix . $_SERVER['REQUEST_URI']);
            header('Location: https://m.' . $config['mobileurl'] . $langPrefix . $_SERVER['REQUEST_URI']);
            //header('Location: 'http://m.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
            exit;
        }
    }
}
if (!isset($_SESSION['lang'])) {
    $_SESSION['lang'] = $config['lang'];
}
if ($_SESSION['lang'] != $config['lang']) {
    if ($_SESSION['lang'] != reset($config['langs'])) {
        $langPrefix = '/' . $_SESSION['lang'];
    } else {
        $langPrefix = '';
    }
    header("Location: " . $langPrefix . $_SERVER['REQUEST_URI']);
    exit;

0 个答案:

没有答案