John Smith
John Smith

Reputation: 495

.htaccess add / support

RewriteEngine On
RewriteBase /myproject/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([^/]+)/$ books.php?id=$1 [QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([^/]+)$ books.php?id=$1 [QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/ books.php [QSA]

Everything works when I type: localhost/name but breaks if I type: localhost/name/

What's wrong with my .htaccess and how do I add support "/"

Upvotes: 2

Views: 47

Answers (1)

anubhava
anubhava

Reputation: 785256

You don't need 2 rules and your 2nd rule has invalid regex anyway. Try this rule:

Options -MultiViews
RewriteEngine On
RewriteBase /myproject/

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^books/([^/]+)/?$ books.php?id=$1 [L,NC,QSA]

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

Upvotes: 1

Related Questions