从URL隐藏目录名称

时间:2013-08-21 15:21:16

标签: .htaccess url-rewriting

我只想隐藏网址中的目录名称。

来自:example.com/dirname/somepage :example.com/somepage

那段代码对我不起作用,我可能犯了一些错误

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule    /  /dir/$1    [L]

我已经在.htaccess(隐藏php扩展名)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

1 个答案:

答案 0 :(得分:11)

这应该是你完整的.htaccess:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dirname/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^dirname/)^(.*)$ /dirname/$1 [L,NC]