【问题标题】:htaccess - 301 redirecting specific file and extension to folder roothtaccess - 301 将特定文件和扩展名重定向到文件夹根目录
【发布时间】:2014-09-03 21:00:56
【问题描述】:

令人惊讶的是,我在任何地方都找不到这个答案(无论如何都是以简单的方式)。

我希望将网站上的一些旧 URL 重定向到新结构。问题是 - 以我的能力,我遇到了问题。

我想要什么:

/oldfolder/example.html 重定向到http://website.com/newfolder/

所以我正在使用这个:

Redirect 301 /oldfolder/example.html http://website.com/newfolder/

问题是,这似乎最终重定向到: http://website.com/newfolder/example.html

而且我似乎无法阻止它在新 URL 的末尾附加旧文件名 /example.html

我也试过了:

RedirectMatch 301 ^/oldfolder/example.html$ http://website.com/newfolder/$

但还是没有运气。

谁能帮我找到正确的语法来从文件重定向到不同的文件夹根目录?

编辑: 由于下面第一个答案中的解决方案似乎没有帮助,我觉得还有更多,所以这里有更多信息......

这是我的 .htaccess 文件:

Options +FollowSymLinks
RewriteEngine On

# WORKING
Redirect 301 /defaultsite http://improvemyrunning.com/
Redirect 301 /book/index.html http://improvemyrunning.com/
Redirect 301 /contact/index.html http://improvemyrunning.com/contact/
Redirect 301 /camps/ http://improvemyrunning.com/run-camps/
Redirect 301 /photos/ http://improvemyrunning.com/
Redirect 301 /assets/neil_feedback.pdf http://improvemyrunning.com/run-camps/
Redirect 301 /team/index.html http://improvemyrunning.com/about/
Redirect 301 /coaching/index.html http://improvemyrunning.com/services/


# BROKEN
Redirect 301 /camps/feedback.html http://improvemyrunning.com/run-camps/
Redirect 301 /camps/itinerary.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/index.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/index2.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/1.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/2.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/3.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/4.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/5.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/6.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/7.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/8.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/9.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/10.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/11.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/12.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/13.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/14.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/15.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/16.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Glenholm%20Countryside/slides/17.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Run%20Training%20Camp%20September%202013/index.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Run%20Training%20Camp%20September%202013/slides/Glenholm_Centre.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Run%20Training%20Camp%20September%202013/slides/Group_Picture.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Run%20Training%20Camp%20September%202013/slides/Beautiful_Weather.html http://improvemyrunning.com/run-camps/
Redirect 301 /photos/Run%20Training%20Camp%20September%202013/slides/Sunday_Trail_Running.html http://improvemyrunning.com/run-camps/


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

任何人都可以看到我遗漏的任何东西吗?也许底部的 Wordpress htaccess 规则有干扰??

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect rewrite


    【解决方案1】:

    你可以使用这条规则:

    RedirectMatch 301 ^/oldfolder/example\.html$ /newfolder/
    

    请务必在新浏览器中进行测试,以避免旧的 301 浏览器缓存。

    如果您使用的是mod_rewrite,那么您可以在您的根 .htaccess 中使用它:

    RewriteEngine On
    
    RewriteRule ^oldfolder/example\.html$ /newfolder/ [L,NC,R=301]
    

    【讨论】:

    • 非常感谢,我相信我的问题肯定还有更多问题,因为这些解决方案中的任何一个都有效。我将详细说明我最初的问题。
    • 我已经添加了我的 htaccess 文件 - 有些事情表明 Wordpress 规则可能正在干扰......
    • 哪个 URL 没有重定向。我建议只使用mod_rewrite 规则,所以不要使用Redirect 规则。
    • 我的 htaccess 文件中# BROKEN 下面的规则未能按预期运行:/
    • 首先从你的.htaccess 中删除所有Redirect 301 行,然后在RewriteBase 行下方添加我建议的RewriteRule 行。
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2017-01-29
    • 2021-09-03
    • 2011-01-19
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多