htaccess 重写 URL,不带.php文件扩展名


htaccess Rewrite URL Without .php Extension To File

我有一个使用osCommerce的网站,所有页面都可以用http://www.example.com/pagename.php直接访问,但现在我想调整htaccess文件,以便它可以支持http://www.example.com/username然后重定向到http://www.example.com/account.php?id=username,而其他页面仍然可以使用相同的旧方式访问。

这意味着,如果 htaccess 检测到该 URL 没有任何扩展名,那么它将重定向到 http://www.example.com/account.php?id=username .

谢谢!

你只需要一个规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /account.php?id=$1 [L,QSA]

您可以使用此代码从 URL 中删除 php 扩展名:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# This will redirect you from http://www.example.com/username.php to 
# http://www.example.com/username externally                                                          
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}'s([^.]+)'.php [NC]
RewriteRule ^ %1 [R,L]
# This will redirect you from http://www.example.com/username to 
# http://www.example.com/username internally   
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]