【问题标题】:using htaccess redirection brings some weirds behaviour使用 htaccess 重定向会带来一些奇怪的行为
【发布时间】:2014-11-08 07:55:44
【问题描述】:

我确实使用 htaccess 来重定向某些页面 301

但它做了一些奇怪的重定向。

为了确定它是否不在我的代码中,我只是放了一个空文件 index.php

但我得到了相同的结果

当我调用 mywebsite.com/accueil.html 时,它会重定向到 mywebsite.com/?p=accueil

这不是我所期望的。

我还删除了那个 301 重定向,但它仍然重定向到这个。

下面是我的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

任何形式的 pf 帮助将不胜感激。

编辑:以下功能似乎是问题

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);
    }
}

【问题讨论】:

  • 重定向永远不会发生,因为第一条规则将执行重写RewriteRule ^([^/]*)\.html$ index.php?p=$1 [L]

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

你的第一条规则:

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

匹配mywebsite.com/accueil.html,后者被重写为mywebsite.com/?p=accueil。 您可以执行以下操作来解决此问题:

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]

【讨论】:

  • 我编辑了我的 htaccess,但是当你继续 sp-batiment.com/accueil.html 它仍然重定向到错误的页面
  • 您是否按照上面的示例添加了RewriteCond(未经测试)?
  • 是的,我复制了你所有的代码# only rewrite if not accueil.html RewriteCond %{REQUEST_URI} !accueil\.html RewriteRule ^([^/]*)\.html$ index.php?p=$1 [L]
  • 稍等,将在本地尝试。
  • 我编辑了我的主要帖子,但我认为我需要创建一个新主题。感谢您的帮助,它确实帮助我了解了问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2011-05-18
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多