【问题标题】:htaccess 404 not working properly while adding trailing slashhtaccess 404 在添加斜杠时无法正常工作
【发布时间】:2015-07-23 12:25:54
【问题描述】:

我会尽可能清楚地解释我的问题。

我有一个带有清理 URL 的网站,它通过 .htaccess 重定向。该网站应该是双语的(fr|en)。 (www.kamelya.ca)

在 htaccess 的帮助下,我为所有内容添加了斜杠(http://kamelya.ca/fr/contact => http://kamelya.ca/fr/contact/ 等)

我还有一个 404 errorHandling 重定向到 fr/404.php 错误页面。

问题是当我试图处理“虚拟”文件夹(2 级和+)中的错误页面时,它会在最后添加很多 .php。例如:http://kamelya.ca/fr/qwerty => http://kamelya.ca/fr/qwerty.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php/(此页面不存在,但我想登陆fr/404.php页面)。

htaccess 正确处理第一级 (http://kamelya.ca/whateverincorrect => http://kamelya.ca/fr/404.php) 但不是后续级别...

这是 htaccess 文件(已清理,对法国 cmets 感到抱歉 :)):

Options +FollowSymlinks
RewriteEngine On

RewriteBase /

DirectoryIndex index.php

# Error
ErrorDocument 404 /fr/404.php

# Add ending slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L] 

# (Don't mind thoses next lines until #END, problem isn't here :) )
# Boutique en ligne 
RewriteRule ^(en|fr)/([0-9a-zA-Z+_-]*)-([0-9]*)/$                       $1/boutique.php?cid=$3 [L]
RewriteRule ^(en|fr)/([0-9a-zA-Z+_-]*)/([0-9a-zA-Z+_-]*)-([0-9]*)/$     $1/fiche.php?id=$4 [L]
# Page principales  
RewriteRule ^(en|fr)/acces-client/$             $1/acces-client/ [L]
RewriteRule ^(en|fr)/acces-client/(.*)/$        $1/acces-client/$2.php [L]
RewriteRule ^(en|fr)/boutique-en-ligne/$        $1/boutique.php [L]
# END 

RewriteRule ^(en|fr)/(.*)/(.*)/$                $1/$3.php [L]
RewriteRule ^(en|fr)/(.*)/$                     $1/$2.php [L]
RewriteRule ^(en|fr)/$                          $1/index.php [L]

我知道最后三行有问题。 (这是我发现处理多级虚拟文件夹的唯一方法......)

我在服务器上的物理文件结构是:

httpdocs
    .htaccess
    fr (folder)
        - index.php
        - contact.php
        - boutique.php
        - fiche.php
        - etc
    -en (folder) (which isn't complete yet, but will get all same structure as fr)

所以,无论我们是什么虚拟级别(级别 1、级别 2 或级别 3),所有“真实”文件都在“fr”文件夹中。


这里有 3 个例子:

1 级 - http://kamelya.ca/fr(404 = 正常:http://kamelya.ca/frr

2 级 - http://kamelya.ca/fr/contact(404 不正常:http://kamelya.ca/fr/contactt

3 级 - http://kamelya.ca/fr/a-propos/pourquoi-choisir-kamelya(404 不正常:http://kamelya.ca/fr/a-propos/pourquoi-choisir-kamelyaa

如果您需要更多说明/解释,请随时提问! (也欢迎更改代码!)

非常感谢您的帮助!

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    这是由于您的尾部斜杠规则而发生的。将该规则更改为:

    # Add ending slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^[^.]+?[^/.]$ %{REQUEST_URI}/ [R=302,L]
    

    如果您的网址中有一个点,这将避免添加尾部斜杠。

    【讨论】:

    • 有效!多谢!附带问题,它是 302 重定向而不是 301 的原因吗?
    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 2020-05-12
    • 2016-04-21
    • 2015-02-15
    • 2011-09-19
    • 2012-01-12
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多