php - htaccess 404 在添加尾部斜杠时无法正常工作

标签 php regex apache .htaccess mod-rewrite

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

我有一个带有干净 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 文件(已清理,对法语评论感到抱歉 :)):

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)

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

非常感谢您的帮助!

最佳答案

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

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

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

关于php - htaccess 404 在添加尾部斜杠时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194107/

相关文章:

php - 将 CSV 文件写入 SQL

c# - 正则表达式 c# 只有正数

apache - Docker-Apache HTTP代理-动态服务器名

php - MailChimp API PHP - 添加到兴趣组

php - 未捕获的 PDOException : could not find driver

javascript - 带有发布数据的请求后,PHP 中的 $_POST 数组为空

php - 如何在 preg_match() 中匹配重音字符?

PHP正则表达式将嵌套()替换为[]

linux - 运行 Apache/Linux 的 AWS EC2 中的密码保护目录

regex - 在子文件夹中重写 .htaccess