大网数据广告活动
服务器运维问答

how to redirect to www while also changing document root folder in htaccess

· 52CE 我爱测

How to create the following rules in .htaccess ? Mod rewrite is enabled. But I am running into problems. For each of the rules in each case (1) and (2) I want them to apply to the domains under that rule. Currently all domains po…

问题原文

原帖链接 · Gary · CC BY-SA 4.0

How to create the following rules in .htaccess ? Mod rewrite is enabled. But I am running into problems.

For each of the rules in each case (1) and (2) I want them to apply to the domains under that rule.

Currently all domains point to /public_html/. The .htaccess file is in /public_html/.htaccess.

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

1. Set /public_html/web/ as root path!
2. Redirect always to https://www, keep URL and parameters!

domain1.com
domain2.com
domain3.com

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

1. Set /public_html/live/ as root path!
2. Redirect always everything to https://www.domain4.net, keep URL and parameters!

domain4.net
domain5.com

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

1. Set /public_html/staging/ as root path!
2. Redirect always everything to  https://www.domain6.com, keep URL and parameters!

domain6.com
domain7.com
domain8.com
domain9.com

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

1. Keep /public_html/ as root path!
2. Redirect always everything to https://www.domain10.com, keep URL and parameters!

domain10.com
domain11.com
domain12.com

When I attempt to solve with the following:

RewriteCond %{HTTP_HOST} ^domain1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain1.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]

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

Whenever the URL is accessed it shows the subfolder in the URL which is not what I want!

I found the following from siteground: https://www.siteground.com/kb/how_to_change_my_document_root_folder_using_an_htaccess_file/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]

and tried simply changing this as follows:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) https://www.domain1.com/folder/$1 [L]

But this also shows the subfolder in the URL.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

A few comments on your attempts...

RewriteCond %{HTTP_HOST} ^domain1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain1.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]

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

These directives are in the wrong order. The redirect needs to go first in order to prevent the /web subdirectory being exposed. However, in its current incarnation, this would result in a redirect loop as it simply redirects to itself again and again... (I guess maybe the exemplified code introduced this "redirect loop"?).

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) https://www.domain1.com/folder/$1 [L]

Rewriting to an absolute URL (ie. with scheme + hostname) will implicitly trigger an external redirect and therefore expose the "hidden" subdirectory. You need to keep the redirect (to www.domain1.com) and the rewrite to /folder as separate rules - these are two different tasks.


My first thought is to treat this as one system. All redirects first, followed by the rewrites. I'll treat the redirect to www as the general case, so I'll include this last.

NB: Test with 302 (temporary) redirects first in order to avoid potential caching issues and only change to 301 (permanent) once you have confirmed everything works OK.

For example:

RewriteEngine On

#
# CANONICAL REDIRECTS
#

# Canonicalise www.domain4.net
RewriteCond %{HTTP_HOST} ^domain4\.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain5\.com [NC]
RewriteRule ^ https://www.domain4.net%{REQUEST_URI} [R=301,L]

# Canonicalise www.domain6.com
RewriteCond %{HTTP_HOST} ^domain6\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain7\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain8\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain9\.com [NC]
RewriteRule ^ https://www.domain6.com%{REQUEST_URI} [R=301,L]

# Canonicalise www.domain10.com
RewriteCond %{HTTP_HOST} ^domain10\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain11\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain12\.com [NC]
RewriteRule ^ https://www.domain10.com%{REQUEST_URI} [R=301,L]

# Redirect to www subdomain all other domains
# NB: If you have other domains that shouldn't be redirected OR other subdomains
#     then you will need to explicitly check the hostnames as above.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#
# INTERNAL REWRITES
# NB: Since the request has already been canonicalised (above)
#     We only need to check the canonical hostnames below.
#

# Rewrite to /live
RewriteCond %{HTTP_HOST} ^www\.domain4\.net
RewriteRule !live/ /live%{REQUEST_URI} [L]

# Rewrite to /staging
RewriteCond %{HTTP_HOST} ^www\.domain6\.com
RewriteRule !staging/ /staging%{REQUEST_URI} [L]

# Rewrite to /web
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain3\.com [NC]
RewriteRule !web/ /web%{REQUEST_URI} [L]

# Keep root as the document root for all remaining domains...

This assumes you are already handling the HTTP to HTTPS redirection elsewhere in your config (preferably before these directives).

However, this is not necessarily complete, as the user could still potentially access the /web, /live or /staging subdirectories directly (for any domain) if they are known. This is probably not an issue if this is a new system where these subdirectories have not yet been exposed. Additional redirects can be implemented to canonicalise this if required. Although, if you have additional .htaccess files in these subdirectories then this redirect would need to be included in the subdirectory .htaccess file instead.

Just to clarify, this doesn't "change the document root folder" - that is only possible by changing the server config. This is simply a URL rewrite that hides the subdirectory. The application still sees the document root as defined by the server config, ie. /public_html/.

UPDATE:

If you do not already have a general HTTP to HTTPS redirect then you should add a separate redirect at the top of the .htaccess file. For example:

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

This redirects from HTTP to HTTPS on the same host (which is actually a requirement should you ever implement HSTS). The redirects that follow then canonicalise the www subdomain as required.

If you have no intention to implement HSTS then you could combine the HTTP to HTTPS and non-www to www redirects into one. And then remove the non-www to www that is currently at the end of the redirect block. For example:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-08-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。