Kaboom
Kaboom

Reputation: 684

.htaccess remove extension but allow 404 if not existing

I have removed file extensions on .php for my webserver. I just wanted to make it look nicer. This works excellent.

I have the following pages: sitename.com/portfolio sitename.com/portfolio/resume/file.html

The htaccess has rules so that when they visit the second link its just converting it to portfolio.php?resume&file=$1. That works awesome.

Okay heres the issue. Because i remove the extensions, if a user were to typ the domain as sitename.com/ portfolio/ with a traing slash, they will get an internal server error. Likewise, if they just type sitename.com/portfolio/r they also get an error since it doesnt exist. I know one way around this is to add an option in htacess for every page but that seems like a bad way of doing it.

Anyways here the current htacess file I am using:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^portfolio/resumes/([^/]+).html$ /portfolio.php?resume&file=$1 [L,QSA]
RewriteRule ^portfolio/resumes$ /portfolio.php?resumes [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
ErrorDocument 404 /404.php

If i visit the site on a page that doesnt exist like sitename.com/1 or sitename.com/1.php then I get my 404 page, but if 1.php DID exist then i would see it and IF it did exist, and I typed sitename.com/1/ or sitename.com/1/somethingelse then instead of a 404 (which I expect) then it gives me an internal server error. Is there any way around this? an error in my syntax? Thanks

Upvotes: 1

Views: 350

Answers (1)

Amit Verma
Amit Verma

Reputation: 41249

This is because when you request an uri with a trailing slash eg : /uri/ , your rule rewrites it to /uri/.php instead of /uri.phpwhich is an unknown/unsupported destination. You need to accept the trailing slash as an optional char. Change your rule to :

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

Upvotes: 1

Related Questions