Question

I have a problem with my htaccess. I want to add a trailing slash to my URL. I'm currently developing locally. So here's the htaccess:

Options +FollowSymlinks
RewriteEngine On

#RewriteBase 
RewriteBase /balade-gourmande

DirectoryIndex index.php

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


# Main pages
RewriteRule ^(.*)/(.*)/$                $2.php [L]
RewriteRule ^(.*)/$                 $1.php [L]

As you can see, I have a lot of other website on my localhost (Why I have a rewritebase). All my .php files are in the "balade-gourmande" directory. I have a index.php and a yes.php file in the balade-gourmande folder.

If I try:

localhost/balade-gourmande/ -> It's ok, I land on index.php

localhost/balade-gourmande/yes -> It's ok, I land on yes.php

localhost/balade-gourmande/yes/ -> It's ok, I land on yes.php

The problem is if I enter something wrong, ex: localhost/balade-gourmande/whatever It give me a: localhost/balade-gourmande/whatever.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php/ with infinite loop error...

Can someone help me with this, if you need more info, I'll try m y best.

Thx!

** Edit: Here's the htaccess located in the root, not in my "balade-gourmande/":

Options +FollowSymlinks
RewriteEngine On

#RewriteBase 

DirectoryIndex index.php

# Error
ErrorDocument 404 '/errorhandler.php'

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


# Page principales
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Was it helpful?

Solution

Replace your 2 rules below # Main pages section with this one:

DirectoryIndex index.php
ErrorDocument 404 /balade-gourmande/errorhandler.php
Options +FollowSymlinks -MultiViews
RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

OTHER TIPS

htaccess

# ----------------------------------------------------------------------
# Start rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.

<IfModule mod_rewrite.c>
 # Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on

# Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^(.*)$ index.php/$1 [L]



</IfModule>

Hope it help you, i never had an error with it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top