首页 > 解决方案 > Apache mod_rewrite 删除 url 和 php 扩展中的文件夹

问题描述

嘿,

我正在尝试使用 apache mod_rewrite 使我的网址更漂亮一些。

目前我的网址如下所示:

https://example.com/sites/profil.php

https://example.com/sites/work.php

我希望它看起来像这样:

https://example.com/profile

https://example.com/work

我怎样才能覆盖“站点文件夹”,所以没有,是否可以删除 .php 扩展名

这就是我的 apache 配置的样子:

<VirtualHost example.com>
DocumentRoot /var/www/example.com/html
ServerName example.com
ServerAlias www.example.com
<Directory "/var/www/example.com/html">
allow from all
Options None
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

在我的文件发生更改后,我是否需要编辑我的链接?

标签: phpapacheurlmod-rewrite

解决方案


这是你的模组重写规则

# Rewrite --- https://example.com/sites/profil.php => https://example.com/profil
RewriteRule ^profil$ sites/profil\.php [NC,L]

# Rewrite --- https://example.com/sites/work.php => https://example.com/work
RewriteRule ^work$ sites/work\.php [NC,L]

将其输入到您.htaccess的正确位置并尝试一下。


推荐阅读