简体   繁体   English

HTACCESS:将域根目录更改为子目录

[英]HTACCESS: Change domain root to sub-directory

I'm trying to use a sub-directory as the root folder for one of my domains. 我正在尝试使用子目录作为我的某个域的根文件夹。 Using .htaccess, I use mod_rewrite's to get the job done. 使用.htaccess,我使用mod_rewrite来完成工作。 Here's the code I already have: 这是我已有的代码:

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]

This gets the job done, but not entirely. 这可以完成工作,但不完全。 For example: 例如:

If I goto http://domain.com/ , it displays index.php from inside the domain folder. 如果我转到http://domain.com/ ,它会从domain文件夹中显示index.php If I goto http://domain.com/test/ , it will display the contents (or 403) of the test folder. 如果我转到http://domain.com/test/ ,它将显示test文件夹的内容(或403)。 BUT if I goto http://domain.com/test (for use of shortcuts, or even to display the folder) I get redirected to http://domain.com/domain/test/ . 如果我转到http://domain.com/test (使用快捷方式,甚至显示文件夹),我会被重定向到http://domain.com/domain/test/

That is not supposed to happen. 这不应该发生。 If anything, it either does a mask from the .htaccess (if test is being used) or should just goto http://domain.com/test/ . 如果有的话,它要么从.htaccess做一个掩码(如果正在使用测试),要么只是转到http://domain.com/test/ I have tried to figure out a way around this, and I cannot. 我试图找到解决这个问题的方法,但事实并非如此。 So I am seeking your help! 所以我正在寻求你的帮助! :) :)

Any and all help is greatly appreciated. 非常感谢任何和所有的帮助。

Try this: its a little crude, but should do what you want. 试试这个:它有点粗糙,但应该做你想要的。 There is a little bit of confusion: some of what I've tried to do will depend on your RewriteBase (you might need to remove one or more / characters). 有一点混乱:我试图做的一些将取决于你的RewriteBase(你可能需要删除一个或多个/字符)。

I've basically added an initial block that specifically looks for directories within your /domain/ folder that don't end with a trailing slash and added one. 我基本上添加了一个初始块,专门查找/ domain /文件夹中的目录,这些目录不以尾部斜杠结尾并添加一个。 Let me know if it works at all. 让我知道它是否有效。

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /domain/$1/

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]

评论者是对的,如果您想use a sub-directory as the root folder for one of my domains ,只需在您的apache虚拟主机配置( DocumentRoot )中配置它。

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(.*)$ /domain/$1/ [L]

The above code successfully does everything I wanted it to do. 上面的代码成功完成了我想要它做的一切。

Use RewriteBase like this: 像这样使用RewriteBase:

RewriteBase /f2f/f2fweb RewriteBase / f2f / f2fweb

What helped me is auto.htaccess generator on my host!!!! 帮助我的是我主机上的auto.htaccess生成器!!!!

DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain.com/TEST//.*$ [NC]
RewriteRule \.(js|css|jpg|gif|png|bmp|mp4|3gp|m4a|m4r|aac|mp3|ogg|wave)$ - [F]

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

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