【问题标题】:301 Redirect entire site to new directory except for index.html landing page301 将整个站点重定向到新目录,除了 index.html 登陆页面
【发布时间】:2013-06-10 12:25:30
【问题描述】:

我正在处理的 wordpress 网站已移至子目录中,因此来自其他网站的所有链接都不再有效。我使用 .htaccess 实现了 301 重定向,这很棒,因为它解决了这个问题,但是旧的根目录现在有一个 index.html,它有我的客户绝对希望看到的登录页面。

那么,我如何设置我的.htaccess 以将所有流量重定向到子目录(以修复传入链接),除了根目录中的 index.html,因为它有登录页面。

我不知道 htaccess 是如何工作的,但这就是我现在所拥有的。

Order deny,allow
ErrorDocument 404 /404.php
DirectoryIndex index.php index.html

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$ [OR]
RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]

谢谢!!

编辑澄清: 现在一切都从根目录重定向到子目录。我希望除根目录中的 index.html 之外的所有内容都重定向。如果用户只请求域名(http://example.com)而不指定页面,我还希望他/她在根目录中的 index.html 页面上提供服务。

【问题讨论】:

  • 请参阅stackoverflow.com/questions/2226364/… 了解可能的解决方案
  • 您可能想要编辑您的问题以显示您所做的工作如何按预期工作或没有按预期工作......您让人们先弄清楚哪里出了问题,然后他们才能开始提出解决方案。
  • 我很抱歉。这更清楚了吗?

标签: .htaccess redirect http-status-code-301


【解决方案1】:

以下代码可以满足您的要求:“如果请求与 index.phpindex.html 或“/”不匹配(即不匹配)(并且匹配不区分大小写),则提供备用位置”

Order deny,allow
ErrorDocument 404 /404.php
DirectoryIndex index.php index.html

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.(php|html) [NC]
RewriteCond %{REQUEST_URI} !^/$ {NC]
RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]

我已经使用优秀的在线测试工具http://htaccess.madewithlove.be对此进行了测试

使用以下测试用例:

http://example.com                -- no rewrite, second condition not met
http://example.com/               -- ditto
http://example.com/index.html     -- first condition not met
http://example.com/index.php      -- first condition not met
http://example.com/some/page.html -- rewritten as http://example.com/portal/some/page.html

编辑你说这仍然没有像预期的那样工作;所以我拿出了大枪。通过打开重写引擎对指令所做的所有事情的“最大日志记录”

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 9

(显然,选择任何你想要的路径),然后在终端窗口中查看日志文件的末尾

tail -f /var/log/apache2/rewrite.log

您可以快速查看哪里有问题。一些摆弄使我得到了以下代码。它说“如果请求的 URI 只是 /index.html/index.php,或者如果它以 /portal 开头,或者如果它是空白的,则不要重定向。

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.(php|html) [NC]
RewriteCond %{REQUEST_URI} !^/portal.*$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule ^(.*)$ http://example.com/portal$0 [R=301,L]

测试用例对我有用 - 看看它们是否对你有用!

注意:我在httpd.conf 文件中进行了这些更改,而不是在根目录的.htaccess 文件中。您需要小心,以便甚至读取根目录中的 .htaccess 文件 - 默认 Apache 配置具有该目录的 Override none,因此需要一些额外的工作。通过将此配置更改放在httpd.conf 文件中(并发出sudo apachectl restart 命令),您可以避免困难。根据谁在托管您的网站以及您拥有什么控制权,这可能不是您的选择。可能会在superuser.com而不是SO上找到解决此问题的专家...但我希望这对您有所帮助。

【讨论】:

  • 太棒了!如果我准确地指定 example.com/index.html 作为我的地址,它就可以工作。如果我只是输入 example.com,那么它会跳过 index.html 并开始重定向。我有办法解决这个问题吗?非常感谢!
  • 澄清一下:如果人们请求根目录(没有扩展名),您也希望将他们重定向到 index.html - 而不仅仅是当他们请求 index.html 时?
  • 正是我想要的:D
  • 只是一个快速更新。我们试过这个:code RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteCond %{REQUEST_URI} !^/index\.(php|html)$ [NC] RewriteRule ^(.*)$ http://example.com/portal/$1 [R=301,L] ,它也不能正常工作。仍然需要专门编写 index.html。
  • 很遗憾这也没有用。 :( 不过我真的很感谢你的帮助!
猜你喜欢
  • 2013-05-19
  • 1970-01-01
  • 2015-04-11
  • 2017-01-08
  • 2016-08-19
  • 2016-06-07
  • 2014-12-16
  • 2015-08-22
  • 2011-03-27
相关资源
最近更新 更多