.htaccess Rewrite .php to 'virtual folder' [duplicate]

此生再无相见时 提交于 2019-12-12 04:14:20

问题


Possible Duplicate:
remove .php extension with .htaccess

I have

http://www.example.com/test/categoryform.php

In my .htaccess file, how do I rewrite that to display as:

http://www.example.com/test/categoryform/

回答1:


try this code out

RewriteEngine On
# turn on the mod_rewrite engine

RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename

and How to hide .php extension in .htaccess question is also usefull




回答2:


In order to remove the trailing slash, you need to match it out in a condition

RewriteEngine On
# make sure it's not a directory or a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# match out the request URI without the trailing slash
RewriteCond %{REQUEST_URI} ^/([^/]+?)/?$
# and see if it exists
RewriteCond %{DOCUMENT_ROOT}/%1.php -f

RewriteRule ^(.+?)/?$ /$1.php [L]

But in order to get it to display (as in, it show up in the browser's address bar, you need:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+?)\.php
RewriteRule ^(.+?)\.php$ /$1/ [L,R=301]



回答3:


Easy question asked a thousand times

RewriteEngine on
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Source: Removing file extension via .htaccess



来源:https://stackoverflow.com/questions/12138697/htaccess-rewrite-php-to-virtual-folder

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