首页 > 解决方案 > 子域到子文件夹 .htaccess 的 RewriteRule

问题描述

我正在尝试完成以下任务:

  1. 对 https 的非 http 强制 -有效
  2. www 强制非 www -作品
  3. 从子文件夹 (/web) 加载的网站 -有效
  4. test.example.com 加载不同的子文件夹 (/test) - 不起作用

4、不行,满足条件去/web。无法理解如何将其更改为 /test

我使用的 .htaccess 代码:

RewriteEngine On
RewriteCond %{HTTPS} !=on

RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ /test/$1 [L,NC,QSA]

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteRule ^(.*)$ /example/web/$1 [L,NC,QSA]

标签: .htaccessredirectmod-rewriteurl-rewritingfriendly-url

解决方案


使用您显示的示例,请尝试在此处遵循 htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。如果您有更多规则(除了显示的规则),请确保这些规则在这些规则之前。

RewriteEngine On
##Apply https to uris here..
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

##Apply non-www to uris here..
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]

##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ example/web/$1 [L,NC,QSA]

##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ www.example.com/test/$1 [L,NC,QSA]

推荐阅读