Apache mod_rewrite删除网址和php扩展名中的文件夹

时间:2018-09-07 13:53:49

标签: php apache url mod-rewrite

嘿,

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

  • html(根)
    • css
    • js
    • 布局
    • 网站(文件夹)
      • profil.php
      • work.php
    • index.php

此刻,我的网址如下:

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

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

我希望它看起来像这样:

https://example.com/profil

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>

文件更改后是否需要编辑链接?

2 个答案:

答案 0 :(得分:0)

这是您的mod重写规则

# 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中,然后尝试一下。

答案 1 :(得分:0)

while