【问题标题】:Redirect folder access to file (but not redirect files in folder)重定向文件夹访问文件(但不重定向文件夹中的文件)
【发布时间】:2015-08-24 17:31:47
【问题描述】:

我目前在abc.com/folder/ 设置了一个网页,该文件夹中有多个.php 文件需要访问。由于平滑滚动脚本和我的共享导航文件之间存在冲突,我正在寻找将abc.com/folder/ 重定向到abc.com/folder/index.php,但不会重定向abc.com/folder/xyz.php 的.htaccess 规则。目前我有:

RewriteEngine On
RewriteRule ^/?folder/ ^/folder/index.php [R=301,L]

但这会将所有文件重定向到index.php,而我只想重定向基本目录但没有任何文件。不会改变任何东西。

编辑:目前我有这个:

RewriteEngine On
RewriteRule ^/?folder/ ^/folder/index.php [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f

它根本没有做任何事情。

【问题讨论】:

  • 我不太确定您的服务器是如何设置的,但大多数 LAMP 服务器会假定对 abc.com/folder/ 的请求是针对 abc.com/folder/index.php
  • 它确实加载了正确的页面,但我的问题是,当我在 /folder/ 并尝试转到 /folder/index.php#about 时,它必须重新加载页面,所以我的平滑滚动脚本不起作用。我想要从/folder//folder.php 的硬重定向

标签: php .htaccess redirect


【解决方案1】:

您可以在 root .htaccess 中使用这个简单的规则:

RedirectMatch 301 ^/folder/?$ /folder/index.php

当 URI 为 /folder/ 时,这将重定向到 /folder/index.php

【讨论】:

  • 我正在寻找一个硬重定向,它会加载/folder/index.php 的内容,但是当我访问/folder/index.php#about 时,它仍然会在第一次重新加载页面,因为它将它视为一个新文件。
  • 但是/folder/index.php#about/folder/index.php 的行为都是一样的,因为网络服务器甚至不知道#about
  • 但是你的规则对我来说仍然是/folder/,它不会硬重定向到/folder/index.php
  • 您可以添加这样的规则:RedirectMatch 301 ^/folder/?$ /folder/index.php 以获得硬重定向。
【解决方案2】:

当请求的文件存在时,以下条件将告诉 Apache 不要重写。

RewriteCond %{REQUEST_FILENAME} !-f

如果您有其他类型的重写规则,或者您将所有内容发送到单个文件,这很有用。

【讨论】:

  • 我现在的问题是规则的行为就像它们甚至不存在一样(即使没有您的编辑,我认为我的原始帖子是错误的)。我需要从/folder//folder/index.php 的硬重定向
【解决方案3】:

只是在我的脑海中,但是怎么样

重写规则 ^/?tradeshow/$ ^/tradeshow/index.php [R=301,L]

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多