使用mod_rewrite和RewriteRule发生内部服务器错误

问题描述:

我试图在我的网站上使用mod_rewrite做两件事。使用mod_rewrite和RewriteRule发生内部服务器错误

首先,我想隐藏的URL PHP文件扩展名.. (例如website.com/about.phpwebsite.com/about) 和我有这么多正常工作。

但我也想简化网址查询和争论,使他们更加搜索引擎优化友好。 (例如website.com/work/item.php?id=item-slug成为website.com/work/item-slug

这是我的.htaccess文件,到目前为止,但它给我的内部服务器错误...

<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> 
+0

您确定要将“website.com/work/item.php?id=item-slug”转换为“website.com/work/item-slug”吗?我想,你想要“website.com/work/item/item-slug”。 –

你得到500错误,因为*.是无效的正则表达式不能被编译。此外,您需要跳过上次重写规则中的文件/目录。

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] 
+1

非常感谢!工作完美无缺:) – Splycd