dongza3124 2014-11-07 23: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 00: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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)