htaccess重写规则一个域中的所有现有文件和目录都转到另一个域中的相同文件路径

问题描述 投票:0回答:1

我正在尝试编写一个htaccess重写规则,将域http://或https://以及www或者不是lun.re的所有现有文件和目录发送到lunre.com。

所以确实存在的lun.re/admin会进入lunre.com,但是不存在的lun.re/new会留在lun.re,但会重定向到https://lun.re/new

这是我到目前为止所拥有的:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^lun\.re$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lun\.re$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://lunre.com/ [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

第5-8行已经从我的托管服务提供商处获得。

我也试过这个:

RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /loader.php [L,R]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]

但后来我得到了太多重定向错误。

我究竟想要做的是,如果它确实存在且域名是lun.re然后转到https://lunre.com/file-or-dir(长URL上的相同文件路径),否则如果它不存在则停留在当前域并转到https://currentdomain/loader.php

apache .htaccess
1个回答
0
投票

你可以用这个:

RewriteEngine on

# http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
#redirect all existing files/directories to http://lunre.com
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]

您不需要RewriteRule将不存在的请求重定向回主域,因为规则仅重定向现有文件/文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.