jay
jay

Reputation: 10325

500 error for clean url mod re-write

Thanks to everyone who has helped me with my clean URL quest. I'm almost there but want to lick this last thing.

.htaccess

RewriteEngine on

#REMOVE THIS LINE ON SITE LAUNCH!
RewriteBase /~accountName/

#Hide .php extensions for cleaner URLS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

#calendar-expanded REST-ful URLs
RewriteRule ^calendar-expanded(/.*)?$ calendar-expanded.php$1 [L]

Options All -Indexes

<IfModule mod_security.c>
    SecFilterEngine Off
    SecFilterScanPOST Off
</IfModule>

# propagate php.ini file to all sub-directories
suPHP_ConfigPath /home/accountName/public_html/php.ini

my working url

www.example.com/calendar-expanded/4/mother-mother-in-concert/

a broken url

www.example.com/calendar/12/another-band-live/

There is no mod re-write for calendar/ obviously. My question is, do I absolutely need a rule for every page to avoid internal server errors if someone accidentally browses to the wrong page? The broken url is an example of that. I'd really love for the browser to return a 404 error instead.

I'm looking for a cond/rule that will cover "all other" urls except calendar-expanded. Is this doable?

Thanks. (@Gumbo I'm looking hopefully in your direction!)

Upvotes: 0

Views: 220

Answers (2)

Gumbo
Gumbo

Reputation: 655785

I could reconstruct this behavior and trace it back to this condition:

RewriteCond %{REQUEST_FILENAME}\.php -f

Here REQUEST_FILENAME does only contain the filesystem path to calendar within the same directory. And that appended with .php is an existing file so the condition is true. But $1 does contain the full requested URL path that is now appended .php. This keeps repeating until the internal counter exceeds its limit.

A solution to that is to use %{REQUEST_FILENAME}.php in the substitution as well:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php

Upvotes: 2

Nathan Osman
Nathan Osman

Reputation: 73295

Could you create a rule that matches everything and just have it at the bottom? The rewrite engine gives precedence to rules based on the order in which they appear in the .htaccess file.

Upvotes: 0

Related Questions