Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess 404 not working properly while adding trailing slash

I'll try to explain my problem the most clearly possible.

I have a website with cleans URL, which is redirected via .htaccess. The website is supposed to be bilingual (fr|en). (www.kamelya.ca)

With the help of htaccess, I add a trailing slash to everything (http://kamelya.ca/fr/contact => http://kamelya.ca/fr/contact/, etc)

I also have a 404 errorHandling to redirect to a fr/404.php error page.

The problem is when I'm trying to handle error pages in "virtual" folders, (level 2 and +), it add a lot of .php at the end. Ex.: 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/ (This page do not exist, but I want to land on the fr/404.php page).

The htaccess handle correctly the first level (http://kamelya.ca/whateverincorrect => http://kamelya.ca/fr/404.php) but not the subsequents levels...

Here's the htaccess file (cleaned, sorry for the french comments :) ):

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]

I know the three last lines are problematic. (This is the only way I've found to handle multiple levels of virtual folders...)

My physical file structure on server is:

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)

So, whatever virtual level we are, (level 1, level 2 or level 3) all the "real" files are in "fr" folder.


Here's 3 examples:

Level 1 - http://kamelya.ca/fr (404 = OK : http://kamelya.ca/frr)

Level 2 - http://kamelya.ca/fr/contact (404 NOT OK : http://kamelya.ca/fr/contactt)

Level 3 - http://kamelya.ca/fr/a-propos/pourquoi-choisir-kamelya (404 NOT OK : http://kamelya.ca/fr/a-propos/pourquoi-choisir-kamelyaa)

If you need more clarifications/explanations, feel free to ask! (Changes in code are also welcomed!)

Thx a lot for your help!

like image 819
DGK.ca Avatar asked Apr 13 '26 04:04

DGK.ca


1 Answers

That is happening due to your trailing slash rule. Change that rule to:

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

This will avoid adding trailing slash if there is a dot in your URL.

like image 123
anubhava Avatar answered Apr 14 '26 16:04

anubhava