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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来