Splycd Splycd - 1 year ago 82
PHP Question

Internal Server Error with mod_rewrite and RewriteRule

I'm trying to use mod_rewrite for two things on my website.

Firstly, I want to hide .php file extensions in the URL..
(E.g. website.com/about.php is website.com/about)
And I have this much working correctly.

But I also want to simplify URLs with queries and arguments to make them more SEO friendly.
(E.g. website.com/work/item.php?id=item-slug becomes website.com/work/item-slug)

This is my .htaccess file so far, but its giving me an Internal Server Error...

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

# Interpret "work/(ARG)" as "work/item.php?id=(ARG)"
RewriteRule ^work/(*.)$ work/item.php?id=$1 [L]

Answer Source

You are getting 500 error because *. is an invalid regex that cannot be compiled. Moreover you need to skip files/directories from your last rewrite rule.

Options -MultiViews
RewriteEngine On
RewriteBase /

# Interpret "work/(ARG)" as "work/item.php?id=(ARG)"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^work/([\w-]+)/?$ work/item.php?id=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download