使用.htaccess隐藏URL中的子文件夹

时间:2017-04-07 19:50:00

标签: apache .htaccess mod-rewrite url-rewriting

我有这样的网址结构:www.mydomain.com/foo/bar/index

我希望实现这一目标:www.mydomain.com/bar/index

我已经有这样的重写规则从网址中删除.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php

那么,如何根据已实施的规则添加新规则来隐藏foo子文件夹?

2 个答案:

答案 0 :(得分:0)

你可以用它。把它放在你现有的RewriteRules

之前
RewriteEngine on
RewriteRule ^bar/ /foo%{REQUEST_URI} [L]

答案 1 :(得分:0)

你可以将你的htaccess作为这个:

RewriteEngine On

# add /foo/ if not there
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^foo/ foo%{REQUEST_URI} [L,NC]

# add .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]