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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5986473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:06:15  来源:igfitidea点击:

HTACCESS: Change domain root to sub-directory

php.htaccessdirectoryroot

提问by Brian Graham

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. Here's the code I already have:

我正在尝试使用子目录作为我的域之一的根文件夹。使用 .htaccess,我使用 mod_rewrite's 来完成工作。这是我已经拥有的代码:

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

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.phpfrom inside the domainfolder. If I goto http://domain.com/test/, it will display the contents (or 403) of the testfolder. BUTif 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/,它会index.phpdomain文件夹内部显示。如果我转到http://domain.com/test/,它将显示test文件夹的内容(或 403)。但是,如果我转到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/. I have tried to figure out a way around this, and I cannot. So I am seeking your help! :)

这不应该发生。如果有的话,它要么从 .htaccess 做一个掩码(如果正在使用测试),要么应该直接转到http://domain.com/test/。我试图想办法解决这个问题,但我不能。所以我在寻求你的帮助!:)

Any and all help is greatly appreciated.

非常感谢任何和所有帮助。

采纳答案by Femi

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. Let me know if it works at all.

我基本上添加了一个初始块,专门查找/domain/文件夹中不以斜杠结尾的目录,并添加了一个。让我知道它是否有效。

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

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

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

回答by leonbloy

The commenter is right, if you want to use a sub-directory as the root folder for one of my domains, just configure it in you apache virtual host configuration (DocumentRoot).

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

回答by Brian Graham

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

The above code successfully does everything I wanted it to do.

上面的代码成功地完成了我想要它做的一切。

回答by Hercules S.

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]

回答by Waqar Alamgir

Use RewriteBase like this:

像这样使用 RewriteBase:

RewriteBase /f2f/f2fweb

RewriteBase /f2f/f2fweb