dongza3124 2014-11-08 07:55
浏览 39
已采纳

使用htaccess重定向会带来一些奇怪的行为

I do use htaccess for redirecting 301 some page

but it does some weirds redirection.

To be sure if it was not on my code, I just put an empty file index.php

but I had the same results

when I call mywebsite.com/accueil.html it redirects to mywebsite.com/?p=accueil

it is not what I'm expected.

I also removed that 301 redirection, but it still redirect to this.

below is my htaccess

Options +FollowSymLinks
RewriteEngine on

#Pages principale après index.php?p=ma-page
RewriteRule ^([^/]*)\.html$ index.php?p=$1 [L]

#Page de visualisation des chantiers 
RewriteRule ^chantier/([^/]*)/([^/]*)\.html$ index.php?

p=chantier&id=$1&titre=$2 [L]

#Page de demande de devis
RewriteRule demande-devis/([^/]*)/([^/]*)\.html$ index.php?

p=demande-devis&type=$1&categorie=$2 [L]

#Le permis de construire
RewriteRule permis-de-construire/([^/]*)/([^/]*)\.html$ index.php?

dossier=permis-de-construire&fichier=$1&p=$2 [L]

#Maisons en bois page générale
RewriteRule maisons-en-bois/([^/]*)\.html$ index.php?p=maisons-en-

bois&module=$1 [L]

#Détail maisons en bois
RewriteRule maisons-en-bois/voir-detail/([^/]*)/([^/]*)/([^/]

*)\.html$ index.php?p=maisons-en-bois&module=voir-

detail&categorie=$1&id=$2&name=$3 [L]

#Maisons en bois par categories
RewriteRule maisons-en-bois/nos_modeles/([^/]*)\.html$ index.php?

p=maisons-en-bois&module=nos_modeles&categorie_home=$1 [L]

#Demande de devis sans categorie
RewriteRule demande-devis/([^/]*)\.html$ index.php?p=demande-

devis&type=$1 [L]

#Articles du blog à lire
RewriteRule lire-article/([^/]*)/([^/]*)\.html$ index.php?p=lire-

article&id=$1&titre=$2 [L]


#404
ErrorDocument 404 /404.html


#301
Redirect 301 /home.html /accueil.html
Redirect 301 /default.html /accueil.html
Redirect 301 /index.html /accueil.html

anykind pf help will be much appreciated.

Edit : following functions seems to be the problem

function www() {
    $protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER['REQUEST_URI'] == '/' || empty($_SERVER['REQUEST_URI']) || is_null($_SERVER['REQUEST_URI'])) {
        if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {

            header('Location: ' . $protocol . 'www.' . $_SERVER['HTTP_HOST']);
            exit;
        }
    } else {
        if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {

            header('Location: ' . $protocol . 'www.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
            exit;
        }
    }
}

function trail() {
    $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    if (substr($url, -1) == '/') {
        $url2 = rtrim($url, '/');
        header('location:' . $url2);
    }
}
  • 写回答

1条回答 默认 最新

  • dongtuanzi1080 2014-11-08 08:13
    关注

    You first rule:

    RewriteRule ^([^/]*)\.html$ index.php?p=$1 [L]
    

    matches mywebsite.com/accueil.html which get rewritten to mywebsite.com/?p=accueil. You can do something like this to work around this behavior:

    Redirect 301 /home.html /accueil.html
    Redirect 301 /default.html /accueil.html
    Redirect 301 /index.html /accueil.html
    
    # only rewrite if not accueil.html
    RewriteCond %{REQUEST_URI} !accueil\.html
    RewriteRule ^([^/]*)\.html$ index.php?p=$1 [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波