2015-04-01 72 views
1

我从这个post复制Zend的重写代码。如何将文档根据“移动”到公共目录一级?

RewriteEngine On 

RewriteRule ^\.htaccess$ - [F] 

RewriteCond %{REQUEST_URI} ="" 
RewriteRule ^.*$ /public/index.php [NC,L] 

RewriteCond %{REQUEST_URI} !^/public/.*$ 
RewriteRule ^(.*)$ /public/$1 

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^.*$ - [NC,L] 

RewriteRule ^public/.*$ /public/index.php [NC,L] 

它的想法是将文档根目录上一级的公共目录不接触或修改虚拟主机文件

所以我在我的本地主机上的一个虚拟项目上测试它,看它是否在我的本地机器上运行。 但它不是。我仍然需要点击公共文件夹才能看到网页内容。

这是我的目录,

.htaccess 
public/ 
    .htaccess 
    index.php 

的index.php,

<?php 

// Show all information, defaults to INFO_ALL 
phpinfo(); 

而且我测试了我的生活的服务器上也是如此。重写代码根本不会将文档根级别移动到公共目录。

任何想法,我已经错过了?

我在我的Zend(Zend 2)项目上测试过,它也不能工作。而且我仍然需要点击公共文件夹才能看到网页内容。

任何想法?

编辑:/public/.htaccess的

内容

RewriteEngine On 
# The following rule tells Apache that if the requested filename 
# exists, simply serve it. 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do 
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work 
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution. 
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ 
RewriteRule ^(.*) - [E=BASE:%1] 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L] 

我不得不使用这个网址,看到网页,

http://{localhost}/a-project/public/ 

代替,

http://{localhost}/a-project/ 

它是活的服务器上一样,我必须使用

http://my-website.com/public/ 

代替,

http://my-website.com/ 

我认为的.htaccess根可以达到一个水平移动文档根到公共目录不接触或修改虚拟主机文件,但它不能明显...

+0

你能告诉'/公/ .htaccess'的内容?还要澄清哪些网址无法使用? – anubhava 2015-04-01 19:46:07

+0

@anubhava请参阅我上面的修改。 – laukok 2015-04-02 03:51:02

回答

1

试试这个规则DocumentRoot .H taccess:

RewriteEngine On 
RewriteBase/

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule (.*) public/$1 [L] 
1

您不应该上传public_html目录中的项目。您应该将您的项目上传到父目录并将您的public目录重命名为public_html

请注意,某些服务器的文档根目录不是public_html。它可能是像wwwhtdocs,..

这种分离是为了更安全,您强制用户发送请求index.php,他们无法访问父目录并直接运行脚本。

*您也可以使用.htaccess,但我不建议这样

+1

'这种分离是为了更安全'我完全同意这一点。 '你也可以使用.htaccess,但我不推荐它',所以它不安全**那么如果使用'.htaccess'将文档根目录“移动”到公共目录一级? – laukok 2015-04-03 08:25:48

+1

我想在我的应用程序的根目录中使用'.htaccess',因为有些服务器没有'public_html','www'或'htdocs' - 如果您曾经听说过one.com – laukok 2015-04-03 08:27:24

+1

如果您将所有请求转发给公众文件夹可以保护您的网站免受许多攻击,但是如果.htaccess配置不正确,则可能会保留一些安全问题。例如在某些情况下攻击者可以找到网站结构(目录)并猜测你的框架。 – 2015-04-03 12:25:08

相关问题