Apache URL重写 - 仅在访问特定路径时隐藏URL中的路径

时间:2017-08-17 18:36:52

标签: apache .htaccess mod-rewrite

我想在输入以下内容时重写我的网址:

www.domain.com/dir1/dir2/dir3/

在该路径中有一个index.html文件将自动加载。我希望它能成为现实:

www.domain.com/index.html

重要的是,根www.domain.com不会重定向到该子目录,因为它已经重定向到另一个.php文件。

我写了以下.htaccess文件:

RewriteRule ^dir1/dir2/dir3/(.*)$ dir1/dir2/dir3/$1 [L]

但即使在键入根domain.com时也会重定向。

你可以帮帮我吗?

修改

我在另一个浏览器中尝试过,真正的问题是,在键入domain.com/dir1/dir2/dir3domain.com/dir1/dir2/dir3/index.html时,dir1/dir2/dir3/仍会显示且未隐藏。

编辑2

我的完整.htaccess

AddHandler application/x-httpd-php56s .php

ErrorDocument 404 /404/404.html
ErrorDocument 500 /404/404.html

RewriteEngine on
RewriteBase /

Options -Indexes

RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404/404.html [R=404,NC,L]

RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404/404.html [R=500,NC,L]

由于

1 个答案:

答案 0 :(得分:1)

用以下代码替换当前代码:

DirectorySlash On
RewriteEngine On

# skip all known resources from rewrite rules below
RewriteRule \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ - [L,NC]

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+dir1/dir2/dir3/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^(?!dir1/dir2/dir3/)(.*)$ dir1/dir2/dir3/$1 [L,NC]