简体   繁体   English

通过httpd.conf / htaccess重定向目录中除一个文件之外的所有文件

[英]Redirect all but one file in a directory via httpd.conf / htaccess

I would like to redirect as such... 我想重定向......

http://old.com/a/b/ -> http://new.com/y/z/
http://old.com/a/b/file.php -> http://new.com/y/z/
http://old.com/a/b/c/file.php -> http://new.com/y/z/
http://old.com/a/b/anything -> http://new.com/y/z/
http://old.com/a/b/EXCLUDE.php -> http://old.com/a/b/EXCLUDE.php

I currently have the following in httpd.conf and it redirects correctly: 我目前在httpd.conf中有以下内容,它正确地重定向:

RedirectMatch 301 /a/b/(.*) http://new.com/y/z/

I don't know how to exclude one file from being redirected. 我不知道如何排除一个文件被重定向。

Basically I want all URL's starting with "old.com/a/b/" to go to a singe new URL, except I want a single URL to be ignored. 基本上我希望所有 “old.com/a/b/” 开头的 URL都转到一个新的URL,除了我想要忽略一个URL。

Using a negative lookahead in the regular expression should work: 在正则表达式中使用否定前瞻应该有效:

RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/

If you want the rest of the path to carry over with the redirect, use the backreference $1 as in: 如果您希望路径的其余部分继续使用重定向,请使用后向引用$ 1,如下所示:

RedirectMatch 301 /a/b/(?!EXCLUDE.php) http://new.com/y/z/$1

I know it's been answered but for people who want some RewriteRule stuff: 我知道它已被回答,但对于想要一些RewriteRule的人来说:

http://old.com/a/b/ -> http://new.com/y/z/
http://old.com/a/b/file.php -> http://new.com/y/z/
http://old.com/a/b/c/file.php -> http://new.com/y/z/
http://old.com/a/b/anything -> http://new.com/y/z/
http://old.com/a/b/EXCLUDE.php -> http://old.com/a/b/EXCLUDE.php

This should work: 这应该工作:

RewriteCond %{HTTP_HOST} ^old\.com [NC]
RewriteCond %{REQUEST_URI} !^(/a/b/EXCLUDE\.php) [NC]
RewriteRule /a/b/(.*) http://new.com/y/z$1 [QSA,NC,R=301]

暂无
暂无

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

相关问题 使用httpd.conf将所有流量重定向到一个子目录 - Redirect all traffic to one sub-directory with httpd.conf .htaccess或httpd.conf - .htaccess or httpd.conf 如何使用htaccess或httpd.conf将所有子目录重定向到root? - How can I redirect all sub-directories to root using htaccess or httpd.conf? apache中禁用httpd.conf与.htaccess中的目录浏览 - Disable directory browsing in httpd.conf versus .htaccess on apaches 启用打开目录(文件夹浏览).htaccess和httpd.conf - Enable open directory (folder browsing) .htaccess and httpd.conf Apache:如何在主机/.htaccess中仅使用一个.htaccess而不使用httpd.conf的情况下,对目录host / content /应用“命令拒绝,允许所有人拒绝”? - Apache: How to apply 'Order Deny,Allow Deny from all' for directory host/content/ with using only one .htaccess in host/.htaccess, without httpd.conf? 尝试编辑“ httpd.conf”文件并将其保存回去时出现错误:“ [[写入/etc/httpd/conf/httpd.conf:无此文件或目录]错误” - Error: “[ Error writing /etc/httpd/conf/httpd.conf: No such file or directory ]” while trying to edit “httpd.conf” file and save it back httpd.conf中的目录是什么? - Whats the directory in the httpd.conf? 通过httpd.conf将登录页面从HTTP重定向到HTTPS - Redirect the login page from HTTP to HTTPS via httpd.conf 将所有URL重定向到httpd.conf中的单个页面 - Redirect all URLs to a single page in the httpd.conf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM