Cristian
Cristian

Reputation: 7145

Internal Server Error when Page Doesn't Exist

I've just realised that if you were to manually type in the URL bar a page that doesn't exist on my site, the site responds with an Internal Server Error.

Should it not be a 404 page not found?

My site is http://www.cristianrgreco.com, for example http://www.cristianrgreco.com/example will return a server error.

This is an error I frequently encountered when toying with .htaccess files so I have posted mine below.

RewriteEngine On
Options +FollowSymLinks 

# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]

# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]

# Rewrite article URLs
RewriteRule ^articles/([a-zA-Z0-9_-]+)/?$ articles.php?article=$1

# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

Thanks in advance for your help.

Upvotes: 0

Views: 171

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

I have encountered this problem before, the problem is this line

RewriteRule ^(.*)$ $1.php [L,QSA]

replacing it with RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA] seems todo the trick. the enclosing brackets signify that anything that is matched will be remembered by the RewriteRule. Inside the brackets, it says “I’d like one or more characters that aren’t a dot”.

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   # Add WWW to URL
   RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
   RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]

   # Remove trailing slashes from end of URL
   RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
   RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]

   # Rewrite article URLs
   RewriteRule ^articles/([a-zA-Z0-9_-]+)/?$ articles.php?article=$1

   # Remove file extension from PHP files
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>

Upvotes: 1

Related Questions