我想用.htacess只重定向一个文件

时间:2016-02-04 00:18:07

标签: apache .htaccess redirect

我的文件需要通过http访问外部源。我的整个网站都是https。如果我执行以下操作:

# Redirect all http traffic to https, only if it is pointed at specialfile.txt
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/specialfile.txt/
RewriteRule ^(.*)$ http://example.com/specialfile.txt$1 [R=301,L]

看起来这会起作用吗?当谈到Apache重写规则时,我是一个绝对的菜鸟,我会RTFM,但我很快就需要这个改变。

我还应该提到已经有了

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

在.htaccess文件中以及一些Wordpress内容。

1 个答案:

答案 0 :(得分:0)

如果你在根文件夹.htaccess文件中添加如下内容,它应该可以工作:

RewriteEngine on

RewriteCond %{REQUEST_URI} specialfile.txt$
RewriteRule ^(.+)$ http://example.com/$1 [R=301,L]

该指令翻译为:

  • 如果请求的URI在specialfile.txt
  • 中结束
  • https://domain.tld/
  • 之后捕获所有字符(从一个到任意数字)
  • 将URI重写为http://domain.tld/all-captured-characters