.htaccess重定向到子文件夹_and_ https

时间:2017-04-28 21:49:03

标签: apache .htaccess redirect mod-rewrite

我在/ public_html / subfolder /中有一个域名,所以我在public_html中使用了带有此内容的.htaccess:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ 
RewriteCond %{REQUEST_URI} !^/subfolder/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /subfolder/$1 
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ 
RewriteRule ^(/)?$ subfolder/index.php [L]

它工作正常,但我想添加额外的重定向到https://

尝试了一些.htaccess修改,但没有运气:我得到重定向循环错误(“太多重定向”)或带有附加部分的URL,如:

https://domains.com/子文件夹 /page.html

代替

https://domains.com/page.html

2 个答案:

答案 0 :(得分:0)

您可以使用以下规则将 http 重定向到 https ,将 root uris 重定向到子文件夹

RewriteEngine on

#http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
#rewrite root requests to subfolder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /subfolder%{REQUEST_URI} [L]

www.example.com 替换为您的域名。

答案 1 :(得分:0)

我有类似的配置,但我在.htaccess中使用了两个条目。您可以在https://snetts.com

查看我的配置

网页内容位于子文件夹中,我正在使用.htaccess强制https://

我的.htaccess如下:

RewriteEngine On

#redirecting non-ssl to ssl:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L] 

#redirecting primary domain to subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^subfolder/ [NC]
RewriteRule ^(.*)$ /subfolder/$1 [L] 
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]

您可以对此进行测试,看看它是否适合您。如果有任何改进可以做,那么请随意。

相关问题