简体   繁体   English

.htaccess-RewriteCond / RewriteRule重定向除特定文件之外的所有文件

[英].htaccess - RewriteCond/RewriteRule to redirect all except a specific file

I have the following in my .htaccess file: 我的.htaccess文件中包含以下内容:

RewriteCond         %{REQUEST_URI}  ^/folder1(/.*|)$
RewriteRule         ^(.*)$  http://folder1.domain.com/  [R=301,L]

This results in taking me to 结果带我去

http://folder1.domain.com

if someone visits 如果有人来

http://www.domain.com/ANYTHINGHERE

I want to keep this functionality. 我想保留此功能。 BUT I want the user to be able to view the file located at: 但是我希望用户能够查看位于以下位置的文件:

http://www.domain.com/folder1/interestingfile.txt

when they visit either 当他们访问任何一个

http://www.domain.com/folder1/interestingfile.txt

OR 要么

http://folder1.domain.com/interestingfile.txt

Any ideas? 有任何想法吗?

You need to add a backreference: 您需要添加反向引用:

RewriteCond         %{REQUEST_URI}  ^/folder1/(.*|)$
RewriteRule         ^(.*)$  http://folder1.domain.com/%1  [R=301,L]

To backrefrence the part of the URI after /folder1/ , assuming that you've got the folder1.domain.com document root pointing to the /folder1/ folder. 为了backrefrence URI的一部分后/folder1/ ,假设你已经得到了folder1.domain.com文档根目录指向/folder1/文件夹。

Have your rules like this: 有这样的规则:

 RewriteEngine On

 RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$ [NC]
 RewriteRule ^(folder1)/(interestingfile\.txt)$ http://$1.domain.com/$2 [R=301,L,NC]

 RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$ [NC]
 RewriteRule ^folder1/ http://folder1.domain.com/ [R=301,L,NC]

Make sure to: 确保:

  1. Keep these rules before any other existing rules 将这些规则保留在任何其他现有规则之前
  2. Test in a new browser to avoid 301 caching 在新的浏览器中测试以避免301缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM