【问题标题】:.htaccess settings for multiple websites with one document root具有一个文档根目录的多个网站的 .htaccess 设置
【发布时间】:2018-03-16 12:11:23
【问题描述】:

My Hoster 允许多个网站,但所有域只有一个 DocumentRoot。

我想要的是具有不同内容的网站:

https://www.site1.com
https://www.site2.com

我有什么:

Directory Structure
/html/ <- Document root
/html/site1.com/ <- Symlink to /pages/site1.com/web
/html/site2.com/ <- Symlink to /pages/site2.com/web

应该如何工作:

Browser: https://www.site1.com/folder/ 
<- this will internally gets internally redirected to /pages/site1.com/web/folder/

目前的 .htaccess 解决方案:

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

如果我访问https://www.site1.com/folder/,它正在工作。

但问题是,我不想要什么:

1.) 如果我访问 site1.com/folder(没有最后一个斜杠),网络服务器会自动重定向到 https://www.site1.com/site1.com/folder/ 而不是 https://www.site1.com/folder/。 我认为这是由于 Apache 目录的自动尾随斜杠功能(具有有限 ssh 帐户的共享 Web 主机)。 一种可能的解决方案是将 301 所有 Web 浏览器 https://site/sitefolder.com/ 请求 uri 重定向到非 /site1.com/ 请求 uri。 但是我测试的每个 htaccess 规则失败

2.) 由于内容重复,我不想让 http://www.site1.com/site1.comhttps://www.site1.com/site2.com 网址可访问。

我怎样才能解决这个问题,并让一个文档根目录具有多个域作为子目录,而不会出现重复的内容/重定向到 url 内的子文件夹?

【问题讨论】:

  • 你能用包括 http 在内的完整 URL 更改示例。如果site1.com/site1.com/folder/ 应该表示http://site1.com/site1.com/folder/http://site1.com/site1.com/site1.com/folder/,我很困惑,如果您的意思是从服务器角度添加的文件夹,请添加/html/ 例如:/html/site1.com/site1.com/folder/

标签: apache .htaccess web webserver


【解决方案1】:

这不能在 3 行中完成,所以我会这样做:

RewriteEngine On

# First we check if we have a direct access to our folder and prevent it with a 404 Error
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?site1\.com(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} ^/?site2\.com(/.*)?$ [NC]
RewriteRule ^.*$ - [R=404,L]

# Optional you can redirect all non-www to www, http or https stay the same
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^/?(.*)$ %{REQUEST_SCHEME}://www.%1/$1 [R=301,L]

# Now we rewriting all request based on the requesting host works with www and non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/?(.*)$ /%1/$1 [L]

答案假定该文件夹的名称与主机名称相同,但没有前导 www.,正如您在问题中所说的那样。所以对于http://www.example.com,文件夹必须是example.com

【讨论】:

    猜你喜欢
    • 2014-02-15
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多