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 python使用pulp线性优化时报错
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题