简体   繁体   English

.htaccess将根重定向到子域

[英].htaccess Redirect Root to Subdomain

I'm attempting to fix some annoyances with my current shared host configuration, but I'm having a few issues getting everything set up as I want it. 我正在尝试使用当前的共享主机配置来解决一些烦恼,但是我在设置所需的设置时遇到了一些问题。

Currently, I have the following folder structure: 当前,我具有以下文件夹结构:

/
/.htaccess
  /example
  /example1
  /example2
  /example3

On the shared host, the primary domain is automatically set up to point to the root directory. 在共享主机上,主域自动设置为指向根目录。 In an effort to point the primary domain to a subdomain, I added the following to my root file's .htaccess: 为了将主域指向子域,我在根文件的.htaccess中添加了以下内容:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{REQUEST_URI} !^/example/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example/ [L]

This properly rewrites the URL so that going to example.com displays the content from the /example/ subdirectory. 这样可以正确地重写URL,以便转到example.com可以显示/ example /子目录中的内容。 The problem is that subdirectories of the root are not being rewritten in the same way, which is not the functionality that I was hoping to achieve. 问题在于根目录的子目录不会以相同的方式重写,这不是我希望实现的功能。 Example: 例:

http://www.example.com/            = /example
http://www.example.com/example/    = /example
http://www.example.com/example1/   = /example1
http://www.example.com/example2/   = /example2
http://www.example.com/example3/   = /example3

What I want to happen is the following: 我想发生的事情如下:

http://www.example.com/            = /example
http://www.example.com/example/    = /example/example
http://www.example.com/example1/   = /example/example1
http://www.example.com/example2/   = /example/example2
http://www.example.com/example3/   = /example/example3

My understanding of what should happen is as follows: 我对应该发生的情况的理解如下:

  1. Request for http://www.example.com received. 收到http://www.example.com的请求。
  2. First condition fails to match, HTTP_HOST does not match ^example.com$ 第一个条件不匹配,HTTP_HOST不匹配^ example.com $
  3. Second condition: matches ^www.example.com 第二个条件:匹配^ www.example.com
  4. Third condition: no REQUEST_URI, so it matches not ^/example/ 第三个条件:没有REQUEST_URI,因此它不匹配^ / example /
  5. Fourth and fifth: I've tried with and without these, but it didn't seem to change the result. 第四点和第五点:我尝试过使用和不使用这些方法,但是它似乎并没有改变结果。 My understanding is that if those lines are uncommented, I would see the unwanted before I'm having right now (if the filename and directory exist, the rewrite would not take place). 我的理解是,如果不注释这些行,那么我现在会看到不需要的行(如果文件名和目录存在,则不会进行重写)。
  6. Rewrite the request to example.com/example/ 将请求重写到example.com/example/
  7. Sixth condition: match the URL to a specific domain. 第六个条件:将URL匹配到特定域。
  8. Always rewrite to use the /example/ subdirectory for this URL. 始终重写此URL,以使用/ example /子目录。

This does appear to work, but if I try to go to http://www.example.com/example2/ , I get the problems. 这看起来确实可行,但是如果我尝试访问http://www.example.com/example2/ ,就会遇到问题。 Since the REQUEST_URI doesn't match /example/, my thought was that it should be rewriting to /example/example2, but that obviously doesn't happen. 由于REQUEST_URI与/ example /不匹配,我以为应该将其重写为/ example / example2,但是显然不会发生。

I don't have access to the Apache configuration or the ability to set up VirtualHosts, so I'm hoping there's some way that I can do this with .htaccess rules. 我没有访问Apache配置的权限,也没有设置VirtualHosts的能力,所以我希望可以使用.htaccess规则进行某些操作。 Any insight into how I'm over-complicating this simple task would be extremely helpful. 对我如何使此简单任务过于复杂的任何见解都将非常有帮助。

As far as I could tell, the .htaccess file in my root was being ignored if a directory in the root matched the request that was coming in (that is, if example1 existed in the root directory, putting a RewriteCond to rewrite /example1 to an error page was never triggered). 据我所知,如果根目录中的目录与传入的请求匹配(即,如果exam​​ple1存在于根目录中,则将RewriteCond重写为/ example1到),则将忽略根目录中的.htaccess文件。错误页面从未被触发)。

My solution to this ended up being a bit less elegant than I had hoped, but it seems to be working. 我对此的解决方案最终没有我期望的那么优雅,但似乎可行。 Hopefully it can help anyone else who stumbles onto this issue. 希望它可以帮助偶然发现此问题的其他任何人。

Inside of the example1, example2, and example3 directories, I modified the .htaccess file to include the following (changing example1 based on the directory): 在example1,example2和example3目录中,我修改了.htaccess文件以包括以下内容(根据目录更改example1):

RewriteCond %{HTTP_HOST} ^(www\.)example\.com$ [NC]
RewriteRule ^(.*)$ /example/example1/$1 [L]

This forces the main domain to serve the content that is within the sub-directory I had created for it, even if there is a matching directory in the root. 这将强制主域提供我为其创建的子目录中的内容,即使根目录中存在匹配的目录也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM