htaccess - 301将特定文件和扩展名重定向到文件夹根目录

时间:2014-09-03 12:51:23

标签: apache .htaccess mod-rewrite redirect rewrite

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

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

我想要的是什么:

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

所以我正在使用它:

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

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

我似乎无法阻止它在新网址的末尾附加旧文件名/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规则是干扰??

1 个答案:

答案 0 :(得分:0)

您可以使用此规则:

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

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

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

RewriteEngine On

RewriteRule ^oldfolder/example\.html$ /newfolder/ [L,NC,R=301]
相关问题