【问题标题】:mod_rewrite redirect from any page to home pagemod_rewrite 从任何页面重定向到主页
【发布时间】:2011-11-04 02:40:52
【问题描述】:

如果有人进入任何文件夹,例如,我需要重定向。 http://site.com/images/http://site.com/imageshttp://site.com。 除非他去归档,例如http://site.com/images/index.php 在这种情况下它不会重定向

现在我用

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]

但我认为它并不完美,因为例如如果有人来http://www.site.com 它不起作用。

请记住,由于服务器配置,我需要将 .htaccess 放在每个文件夹中。

【问题讨论】:

  • 我假设这个 .htaccess 文件在你的 /images/ 目录中?

标签: php html .htaccess mod-rewrite


【解决方案1】:

我相信您正在寻找的是 -f 标志:

RewriteCond %{REQUEST_FILENAME} !-f

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

如果您只想阻止某人获取文件夹中的文件列表,您可能需要考虑添加:

Options -Indexes

如果给定目录中没有索引页,这将告诉 apache 不要显示目录列表。

http://httpd.apache.org/docs/2.2/mod/core.html#options

最后要注意的是,如果您不熟悉 .htaccess 的工作原理,apache 会扫描当前文件夹及其任何父文件夹 - 您应该可以只在您的网络中添加一个 .htaccess 文件http://site.com 的根。这已在另一个问题here 中讨论过。

【讨论】:

  • '-f' (is regular file) Treats the TestString as a pathname and tests whether or not it exists, and is a regular file. ... You can prefix the pattern string with a '!' character (exclamation mark) to specify a non-matching pattern.
  • 在服务器配置中添加单个 .htaccess 对我来说不起作用
猜你喜欢
  • 2021-09-17
  • 1970-01-01
  • 2017-07-09
  • 2015-05-30
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2021-01-28
相关资源
最近更新 更多