简体   繁体   中英

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.. (Eg 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. (Eg 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]
</IfModule>

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM