【问题标题】:.htaccess Always hide a certain subdirectory from URL.htaccess 总是从 URL 中隐藏某个子目录
【发布时间】:2017-02-09 18:13:25
【问题描述】:

我正在尝试使用 .htaccess 从 URL 中隐藏一个子目录。我有一个 PHP 脚本,每次客户端进入网站时都会执行该脚本。它是 Apache 根目录中的 index.php。该脚本确定网站中使用的语言并重定向到目标目录。英语是我的默认语言,所以我需要将 "en" 目录隐藏在 URL 中,同时将所有 URL 请求从根目录重定向到 "en"文件夹,因此它们不会产生 404 HTTP 错误。我通过以下几行部分实现了这一点:

#Remove en/ directory from URL
RewriteRule ^$ en/

#Forward all the requests to en/ directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ en/$1

当请求被转发到 "en" 目录时,它会再次显示在 URL 中。这将是流程:

如何删除 "en" 目录,在列表中的最后一个操作之后留下 URL http://domain.com/panel?有没有更好的方法来管理这种行为?

现行规则:

Options -Indexes +FollowSymlinks -MultiViews

ErrorDocument 404 /404.php

RewriteEngine On

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

#Prevent direct access to PHP Scripts
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]

#Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

【问题讨论】:

    标签: regex apache .htaccess redirect mod-rewrite


    【解决方案1】:

    这样吧:

    Options -Indexes +FollowSymlinks -MultiViews
    ErrorDocument 404 /404.php
    
    RewriteEngine On
    
    #Remove WWW
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
    
    # add a trailing slash if public/$1 is a directory
    RewriteCond %{DOCUMENT_ROOT}/en/$1 -d
    RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
    
    #Prevent direct access to PHP Scripts
    RewriteCond %{THE_REQUEST} \.php[?/\s] [NC]
    RewriteRule ^ - [R=404,L]
    
    RewriteRule ^/?$ en/ [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^((?!en/).*)$ en/$1 [L,NC]
    

    清除浏览器缓存并测试您的流程。

    【讨论】:

    • 我的行为与以前相同。注意:.htaccess 文件位于根目录。
    • 刚刚添加了完整的.htaccess
    • 我也一样。我确保我清除了缓存。额外说明:在尝试访问不是“en”的其他目录时,我收到 404 错误。例如:domain.com/es 给出 404 错误(“es”目录与“en”相同)
    • "es" 目录错误已更正。我仍然得到与以前相同的行为。当我输入“domain.com”时,没有注册 301/302 重定向。当我输入“domain.com/panel”时,301 重定向注册方向为domain.com/panel,发起者为“其他”,下一个 HTTP 代码为 200,地址为 domain.com/en/panel,发起者“domain.com/panel
    • 原始网址http://domain.com/panel重定向网址http://domain.com/en/panel感谢您的耐心等待
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2016-06-26
    • 1970-01-01
    • 2014-06-17
    • 2014-07-08
    • 2016-05-08
    • 2015-07-25
    相关资源
    最近更新 更多