首页 > 解决方案 > htaccess mod_rewrite 转换 url

问题描述

我有一个使用目录结构的 php 项目,我想创建漂亮的 url。基本上我想删除每个文件的“.php”扩展名并删除本地主机和文件名之间的路径内容。

例子:

转换这个 =>localhost/myproject/views/pages/login/login.php

进入这个=>localhost/myproject/login

我知道我必须配置 .htaccess mod_rewrite 但我不知道如何诚实。

标签: php.htaccessmod-rewrite

解决方案


Try and add this to your .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

It should basically remove all the .php extensions from being showed.


推荐阅读