简体   繁体   English

将文件夹中的所有文件重写到另一个文件夹

[英]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.phphttp://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.

相关问题 使用 htaccess 重写所有文件夹和文件 - Rewrite all folder and files with htaccess 不能从一个文件夹重定向(重写)到另一个文件夹 - No redirection (rewrite) from one folder to another folder .htaccess重写规则将所有内容从子文件夹重定向到另一个文件夹 - .htaccess rewrite rule redirect all from subfolder to another folder 将所有请求从一个文件夹重写到父文件夹htaccess - rewrite all requests from one folder to a parent folder htaccess Apache mod_rewrite:从url中仅删除特定文件夹,并从该文件夹内的所有文件中删除扩展名 - Apache mod_rewrite: remove only a specific folder from the url and remove the extension from all the files inside that folder 将文件夹中的所有文件重定向到另一个文件夹-保持查询完整 - Redirect all files in folder to another folder - keep queries intact .htaccess将一个文件夹中的所有文件重定向到另一个文件夹中的完全相同 - .htaccess redirect all files in one folder to exact same in another folder htaccess重写规则,用于将文件夹中的所有图像复制到另一个域 - htaccess rewrite rule for all images in a folder to another domain .htaccess rewriterule文件夹和文件被重写 - .htaccess rewriterule folder and files rewrite 将文件夹重写到该文件夹​​内的另一个子文件夹 - Rewrite folder to another sub-folder inside that folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM