【问题标题】:.htaccess redirect only root of a subfolder to another subfolder?.htaccess 仅将子文件夹的根目录重定向到另一个子文件夹?
【发布时间】:2019-11-02 21:03:36
【问题描述】:

我在“example.com/a”中有一个站点,并且想将 301 重定向,只是将 url “example.com/a”重定向到“example.com/b”,而“example.com/a”中的其他数据不换地址。

在“example.com/a”中创建 .htaccess 并尝试使用以下代码:

RewriteEngine on
RewriteRule ^$ http://www.example.com/b [R=301,L]

RedirectMatch 301 ^/$ https://example.com/b

及其重定向根目录,但“example.com/a”中的所有其他数据显示 404 错误。

我在“root/a”中有很多照片,只是想将“root/a”地址重定向到“root/b”,并且照片的地址保持不变。

例如“root/a/pic.jpg”地址在重定向前后保持不变,

我怎样才能只重定向“example.com/a”而其他旧地址不改变?

【问题讨论】:

  • 您的任何一条规则都应该已经满足您的要求 - 尽管如果 /b 是物理目录,您应该重定向到 /b/(带有尾部斜杠)。 “但example.com/a 中的所有其他数据都显示 404 错误” - 此 redirect 不会导致该错误 - 一定有 其他事情 正在发生。 (确保您的浏览器缓存已清除。)

标签: .htaccess redirect


【解决方案1】:

如果我理解你的话,你需要将root/a 重定向到root/b,同时保持其余页面、文件的位置?

这样就可以了:

# Redirect Root Only
<IfModule mod_rewrite.c>
RewriteEngine On

#Uncheck line below to redirect 'index.php' as well
#Redirect 301 /a/index.php http://example.com/b/

#Redirect root only
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/a/$
Rewriterule ^(.*)$ http://example.com/b/ [L,R=301]
</IfModule>

# END Redirect Root Only

还需要重定向 index.php、index.html 或其他索引页面吗?取消选中上面代码中的行。因为%{REQUEST_URI} ^/a/$ 排除了所有直接文件路径,包括/a/index.html,index.php,index.py等

【讨论】:

  • 不,我在 "root/a" 中有很多照片,只是想将 "root/a" 地址重定向到 "root/b" 并且照片的地址保持不变。比如“root/a/pic.jpg”地址在重定向前后保持不变,
  • @jackson 然后按原样使用代码来实现。
猜你喜欢
  • 2011-01-14
  • 2017-01-29
  • 2021-09-03
  • 2011-01-19
  • 1970-01-01
  • 2012-02-27
  • 2013-02-01
  • 2012-10-23
相关资源
最近更新 更多