htaccess Rewrite URL Without .php Extension To File

匆匆过客 提交于 2019-12-24 02:44:10

问题


I got a website that using osCommerce which all the page can be access directly with http://www.example.com/pagename.php, but now I would like to tweak the htaccess file so that it can supports http://www.example.com/username and then redirecting to http://www.example.com/account.php?id=username, while other pages still can be access using the same old way.

It means that if the htaccess detect that the URL didn't have any extension, then it will be redirecting to http://www.example.com/account.php?id=username.

Thank you!


回答1:


You just need one rule:

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



回答2:


You can use this code to remove php extension from URL:

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]


来源:https://stackoverflow.com/questions/18840581/htaccess-rewrite-url-without-php-extension-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!