htaccess自动重定向,我需要自动重定向的帮助

时间:2019-03-14 11:05:10

标签: php css mysql .htaccess

我需要自动重定向的帮助。 我需要自动重定向到带有www的https,没有www和语言检测的移动设备:

  1. 带有www的https
  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 = '';
}

$ _ SERVER ['REQUEST_URI'] = str_replace($ langPrefix,'',$ _SERVER ['REQUEST_URI']);

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;

1 个答案:

答案 0 :(得分:0)

对于我来说,对于如此复杂的重定向逻辑,您最好使用php端重定向 您可以实现一个函数,根据所有参数准备最终的重定向URL并重定向到该URL。

  • 将所有用户重定向到https版本
  • 检查用户代理以将移动用户重定向到m。
  • 检查接受语言以重定向到正确的语言

类似的东西:

$redirect_url = "https://" . ($is_mobile ? "m." : "www.") . "domain.com/" . $language;
if ($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] != $redirect_url) {
    header("Location: " . $redirect_url); 
}