【问题标题】:Redirect to subfolder invisibly htaccess PHP Apache重定向到子文件夹隐形 htaccess PHP Apache
【发布时间】:2016-04-16 20:32:52
【问题描述】:

我的网站结构如下:

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

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

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

http://localhost/testRedirect then it shall point to `public` folder 

在目录中而不在 URL 中显示 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 [重定向但在 URL 中显示 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

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    堵嘴,

    为此,在运行服务器时,将文档根目录设置为/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。
    • 在“域”部分,单击插件域或子域图标,具体取决于您要修改的内容。
    • 在页面底部的修改域部分,单击您要修改的域的当前文档根旁边的图标。
    • 在弹出的窗口中,输入新的文档根目录并点击更改。

    根据原始海报的详细信息。 BigRock 有一个教程https://support.bigrock.com/index.php?/Knowledgebase/Article/View/613/9/adding-an-additional-domain-to-your-multi-domain-linux-hosting

    【讨论】:

    • 但是如果我使用的是 cPanel,那么我该如何更改虚拟主机文件。如果能做到这一点,那就太酷了
    • 但是为什么我会使用插件域或子域。因为我想在原始域上做。此外,我看不到任何可以编辑的当前文档根目录
    • 你在哪个主机上?
    • 我在 bigrock 主机上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2017-12-05
    • 2015-10-06
    相关资源
    最近更新 更多