[英]Rewrite all files from folder to another folder
I want redirect all requested files from folder-a to folder-b.我想将所有请求的文件从文件夹 a 重定向到文件夹 b。 Eg http://www.yoursite.com/folder-a/index.php to http://www.yoursite.com/folder-b/index.php .例如http://www.yoursite.com/folder-a/index.php到http://www.yoursite.com/folder-b/index.php 。
How can I do that?我怎样才能做到这一点? I have verified if mod_rewrite works with this code:我已经验证了 mod_rewrite 是否适用于此代码:
RewriteEngine On
RewriteRule ^google.html$ http://www.google.com/ [R=301]
The structure on the webspace is the following:网站空间的结构如下:
.htacces .htaccess
|-folder-a |-文件夹-a
|-folder-b |-文件夹-b
But if I want my folder redirect但是如果我想要我的文件夹重定向
RewriteEngine On
RewriteRule ^/folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
the redirect doesn't work if I input the following URL:如果我输入以下 URL,重定向将不起作用:
http://www.yoursite.com/folder-a/ http://www.yoursite.com/folder-a/
http://www.yoursite.com/folder-a/index.php http://www.yoursite.com/folder-a/index.php
The redirect doesn't take place.重定向不会发生。 I stay on the same page ... What I'm doing wrong?我停留在同一页面上......我做错了什么? I also tried it with this htaccess:我也用这个 htaccess 尝试过:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a(.*)$ http://www.yoursite.com/folder-b$1 [L,R=301]
</IfModule>
If all this works I want to exclude some files.如果所有这些都有效,我想排除一些文件。 Eg.例如。 with this htaccess:使用此 htaccess:
RedirectPermanent /folder-a/info.php /folder-b/new-info.php
Edit :编辑:
Now I tried this htaccess现在我尝试了这个 htaccess
redirectMatch 301 ^/folder-a/ http://www.yoursite.com/folder-b
This works, but I need something which takes the whole path and rewrite it to the new folder.这有效,但我需要一些可以获取整个路径并将其重写到新文件夹的东西。
This for example doesn't work:例如,这不起作用:
RewriteRule ^folder-a/(.*)$ folder-b/$1
Solution解决方案
I had an old htacess file in my folder-a
so the redirect didn't worked.我的文件folder-a
-a 中有一个旧的 htacess 文件,因此重定向不起作用。 This is my final htaccess:这是我最后的 htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a/excludefile1.php http://www.yoursite.com/folder-b/newnameforfile1.php [L,R=301]
RewriteRule ^folder-a/excludefile2.php http://www.yoursite.com/folder-b/newnameforfile2.php [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
</IfModule>
You probably need to put both rules in place: one for the empty folder and one for other files, and then the catch-all at the bottom.您可能需要将两个规则都设置到位:一个用于空文件夹,一个用于其他文件,然后是底部的全部规则。 This is not tested:这没有经过测试:
RewriteEngine On
RewriteRule ^folder-a/info.php http://www.yoursite.com/folder-b/new-info.php [L,R=301]
RewriteRule ^folder-a/ http://www.yoursite.com/folder-b/ [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.