Regex 删除htaccess中的php扩展将返回一个内部服务器错误(500)

Regex 删除htaccess中的php扩展将返回一个内部服务器错误(500),regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,我正在尝试创建一个简单的htaccess脚本,将用户重定向到适当的页面。因此,例如,如果文件/文件夹不存在,则导航到/listing/id的用户将显示listing.php?id=id,或者如果他们导航到/username则显示profile.php?id=username 问题是,在访问/listing/id时,我检索到一个内部服务器错误。但这并不是因为处理这一部分的RewriteRule,而是因为我的htaccess脚本中的.php扩展删除部分(我在注释时就发现了这一点)。我不知道为什么这不

我正在尝试创建一个简单的htaccess脚本,将用户重定向到适当的页面。因此,例如,如果文件/文件夹不存在,则导航到
/listing/id
的用户将显示
listing.php?id=id
,或者如果他们导航到
/username
则显示
profile.php?id=username

问题是,在访问
/listing/id
时,我检索到一个内部服务器错误。但这并不是因为处理这一部分的
RewriteRule
,而是因为我的htaccess脚本中的
.php
扩展删除部分(我在注释时就发现了这一点)。我不知道为什么这不起作用,因为我相信我已经设置了正确的标志,并且正在使用正确的代码块

我的代码 案例
  • /
    (索引)有效
  • /search
    有效(文件存在,因此它重定向到搜索页面,而不是名为search的用户)
  • /username
    有效(username.php不存在,因此它会重定向到配置文件页面)
  • /listing/test
    返回内部服务器错误500(删除my htacess的no-PHP扩展部分会导致此页面正常工作,但现在所有其他页面都需要添加
    .PHP


感谢所有帮助,干杯。

按以下顺序尝试您的规则:

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On

# redirect to www.*
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# external profile.php?id=$id to /$id
RewriteCond %{THE_REQUEST} /profile\.php\?id=([\w-]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]

# only allow rewriting to paths that don't exist
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# /listing/$id
RewriteRule ^listing/([\w-]+)/?$ listing.php?id=$1 [L,QSA,NC]

# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# /$username
RewriteRule ^([\w-]+)/?$ profile.php?id=$1 [L,QSA]
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On

# redirect to www.*
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# external profile.php?id=$id to /$id
RewriteCond %{THE_REQUEST} /profile\.php\?id=([\w-]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]

# only allow rewriting to paths that don't exist
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# /listing/$id
RewriteRule ^listing/([\w-]+)/?$ listing.php?id=$1 [L,QSA,NC]

# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# /$username
RewriteRule ^([\w-]+)/?$ profile.php?id=$1 [L,QSA]