Abu Nooh
Abu Nooh

Reputation: 856

trailing slash on url doesnt load page

When I navigate to example.com/books (books.php) the page loads with the intended script however when I add a slash example.com/books/ the page doesn't load with required script, what will I need to change to make sure that it loads with and without the slash?

Here is the content of my .htaccess:

RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico|svg)$ - [L]
RewriteCond %{REQUEST_URI} "/folder/" [OR]
RewriteCond %{REQUEST_URI} "/folder2/" [OR]
RewriteCond %{REQUEST_URI} "/folder3/"
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
RewriteRule ^([A-Za-z0-9-]+)/?$ book.php?slug=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/(.*)/? read.php?slug=$1&cslug=$2 [NC,L]

Upvotes: 1

Views: 87

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Change this part:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

so that it performs an extra check to remove any trailing slash. The

RewriteCond %{REQUEST_FILENAME}\.php -f

condition will fail if you have the URI /book/ because /book/.php doesn't exist.

Try:

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

Upvotes: 1

Related Questions