【发布时间】:2023-04-07 17:16:01
【问题描述】:
我正在尝试使用 .htaccess 文件重写 url,但在某些情况下会引发错误。
我一直在尝试将example.com/room.php 转换为 example.com/room 并且它可以工作,但也可以转换
example.com/room.php?room=5 到 example.com/room/5
RewriteEngine On
RewriteBase /
# Remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Redirect page.php to page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# Internally redirect page to page.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^room/(.*?)$ /room.php?room=$1 [L,NC]
它成功地将room.php 转换为room,当我尝试访问room/5 时购买它给了我500 Internal Server Error。我已经尝试了所有方法,但仍然不知道问题出在哪里。
【问题讨论】:
-
我认为您在这种情况下有额外的扩展名 .php
RewriteCond %{REQUEST_FILENAME}.php -f
标签: regex apache .htaccess mod-rewrite