重定向到子文件夹无形htaccess PHP Apache

时间:2016-04-16 20:32:52

标签: php apache .htaccess redirect mod-rewrite

我的网站结构如下:

root
 |
 |----- .htaccess
 |----- public
          |
          |----- index.php (has all Autoload classes)
 |----- application
          | ---- controllers
          | ---- models etc

现在您可能已经注意到我的Root目录中没有index.php/.html,我故意这样做,因为我希望服务器按.htaccess重定向。

如果您作为用户访问网站说:

,我想实现这一目标
http://localhost/testRedirect then it shall point to `public` folder 

在目录中未在网址中显示public

我尝试过来自SO的解决方案,声称像我想要的那样工作:

解决方案1 ​​ [这只是显示目录结构:(]

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

解决方案2 [给出404 :(]

RewriteEngine on
RedirectMatch ^/$ /public/

解决方案3 [它重定向但在网址中显示public

RewriteEngine on
#RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{HTTP_HOST} ^localhost\$
#RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule (.*) http://localhost/testRedirect/$1 [R=301,L]
RewriteRule ^$ public [L]

请建议一些不错的工作htaccess

1 个答案:

答案 0 :(得分:0)

的GAG,

为此,在运行服务器时,将文档根目录设置为/public文件夹。

如果您使用的是apache ,请将其放在网站设置的虚拟主机中。

DocumentRoot "/path/to/project/public"

参考:https://httpd.apache.org/docs/current/mod/core.html#documentroot

如果您使用的是nginx ,请将以下内容放入网站设置的server{}

root /path/to/project/public;

参考:http://nginx.org/en/docs/

对于cPanel,请执行以下操作

  • 登录cPanel。
  • 在" Domains"部分,单击“添加域”或“子域”图标,具体取决于您要修改的内容。
  • 在页面底部的“修改域”部分中,单击要修改的域的当前文档根目录旁边的图标。
  • 在弹出窗口中,输入新文档根目录,然后单击“更改”。

根据原始海报细节。 BigRock在https://support.bigrock.com/index.php?/Knowledgebase/Article/View/613/9/adding-an-additional-domain-to-your-multi-domain-linux-hosting

上有一个教程