【问题标题】:Mod rewrite redirect folder to root and use folder as rootMod重写重定向文件夹到根并使用文件夹作为根
【发布时间】:2014-09-23 16:00:23
【问题描述】:

我已将 mi 站点文件移动到子文件夹并使用 .htaccess 重定向到它,但我需要拒绝直接访问此文件夹或子文件夹。

在 .htaccess 中得到这个:

<IfModule mod_rewrite.c>

    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  # Block access to hidden files and directories.
  # This includes directories used by version control systems such as Git and SVN.
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

  # Deny direct access to this directory or any sub-directory
    RewriteRule ^site/(.*)$ / [R=301,NC,L]

  # Do not apply redirection to these folders/files for compatibility 
    RewriteRule ^(subdir1|subdir2)(/|$) - [L,NC]
    RewriteRule ^robots.txt - [L,NC]

  # root from sub-directory
    RewriteRule ^(.*)$ site/live/$1 [L,QSA]

</IfModule>

当我去:

htt://domain.com/site -> htt://domain.com (CORRECT REDIRECTING)
htt://domain.com/site/unexist-> htt://domain.com (CORRECT REDIRECTING)
htt://domain.com/site/live -> htt://domain.com/site/live/ (NOT REDIRECTING HERE!)

在 site/live 中存在另一个带有 RewriteRule 的 .htaccess。会不会是这个问题?

<IfModule mod_rewrite.c>

    RewriteRule ^about-us$ about.php
    RewriteRule ^privacy$ privacy.php
    RewriteRule ^oauth$ index.php

</IfModule>

DirectoryIndex index.php

# Custom error messages / pages                                 
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php

【问题讨论】:

  • /site/live/ 是否还有另一个 .htaccess?
  • @anubhava 是的,确实如此,我已经用更多细节更新了这个问题

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

/site/live/.htaccess中有此代码:

DirectoryIndex index.php

# Custom error messages / pages                                 
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php

<IfModule mod_rewrite.c>

    RewriteEngine On

    # remove /site/ from URL
    RewriteCond %{THE_REQUEST} /site/(\S+) [NC]
    RewriteRule ^ /%1 [L,R=302,NE]

    RewriteRule ^about-us$ about.php
    RewriteRule ^privacy$ privacy.php
    RewriteRule ^oauth$ index.php

</IfModule>

【讨论】:

  • 感谢@anubhava,几乎可以工作了。我必须从 RewriteRule ^ /%1 [L,R=302,NE] 中删除 %1 因为它重定向到 /live 女巫不存在 (404)
  • 我的意思是虽然目录存在,但 live 规则不存在,我需要不能从 http://domain.com 以外的地方看到该站点。现在它将http://domain.com/site/live 重定向到http://domain.com,这没关系。
猜你喜欢
  • 2015-10-06
  • 2017-04-09
  • 2023-03-27
  • 2021-11-20
  • 2011-12-20
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 2012-09-22
相关资源
最近更新 更多