如何设置根目录为子文件夹(本地php开发环境)

发布于 2024-12-20 19:09:52 字数 1911 浏览 3 评论 0原文

我刚刚开始学习 Web 开发,但在设置本地开发环境时让自己有点困惑。

我在共享主机上有一个 php 网站有一段时间了。我会编辑本地文件并上传它们,然后刷新以确保一切正常。我知道这是一种顽皮的做法!

因此,我尝试设置本地开发环境并为本地网络服务器(PHP5)安装 AMPPS。 我雄心勃勃地设置了下面的文件夹结构,以便我可以编写一系列网站来尝试不同的语言和框架,同时将它们全部分开。 (方括号表示文件夹)。请注意,确定这是否是正确的方法...(?)

-->[PHP]
---> [AMPPS]
----> [www]
-----> [PROJECTS]

------> [project_name]
-------> index.php
-------> .htaccess
-------> [images]
-------> [section1]
--------> page1.php
-------> [etc...]

------> [project_name]
------> [project_name]
--> [RUBY]
--> [PYTHON]

但是,我的第一个障碍是我的所有链接都是相对的,而且我当然已经通过我的主机在根目录上传了我的网站。我的本地开发环境中的根目录现在是 www,正如您所看到的,它是我安装网站的位置上方的几个文件夹。

链接(例如 Page 1)现在显示为:"localhost/section1/page1.php" 当我相信它们实际上应该显示为 "localhost/PROJECTS/project_name/section1/page1.php" 时。

我不认为告诉 APACHE 我的根目录是 [project_name] 是正确的方法,因为这样它就不适用于我创建的其他项目。我认为我应该在一个项目的基础上做一些事情来指定它自己的根目录。

我认为我可以通过将 .htaccess 文件添加到 project_name 文件夹来指定 root:

根据我的在线研究,我认为它会是这样的:

RewriteEngine on
RewriteRule ^/$ /PROJECTS/project_name/

或者也许

RewriteEngine on
RewriteRule ^/$ .;C/PHP/AMPSS/www/PROJECTS/project_name/

但是我可以'让这些发挥作用。

当网站上线时,我还编写了 ,认为我做的是正确的事情。 现在我不太确定我是否正确理解了我在那里所做的事情。在我的开发环境中,无论有没有它似乎都没有任何影响。而且我无法以任何方式成功编辑它来解决我的相关链接问题。

抱歉发了这么长的帖子。

我感谢所有反馈/帮助。预先感谢:)

[已解决] - 下面的建议是完美的。谢谢大家。 :)

给 Chrome 用户的提示: 如果您使用自定义域(就像我对 .dev 所做的那样),请以 http:// 继续地址,或以尾随 / 结尾。 例如,我现在通过访问projectname.dev/(没有它尝试搜索的最后一个/)来访问我在Chrome中的开发网站。

起初,我以为我错误地设置了 VirtualHost 或主机,但实际上正是 Chrome 的这种行为导致了问题的出现。在撰写本文时,Firefox 等其他浏览器并不存在这种担忧。 希望能帮助别人。

I'm just starting to learn web dev, but got myself a bit confused when setting up my local development environment.

I had a php site on shared hosting for some time. I would edit local files and upload them, then refresh to ensure everything worked. A naughty practice I know!

So I have tried to set up a local dev environment and installed AMPPS for a local webserver (PHP5).
I have ambitiously set up the folder structure below so that I can code up a range of websites to try out different languages and frameworks whilst keeping them all seperate. (Square brackets denote folders). Note sure if this is the correct approach... (?)

-->[PHP]
---> [AMPPS]
----> [www]
-----> [PROJECTS]

------> [project_name]
-------> index.php
-------> .htaccess
-------> [images]
-------> [section1]
--------> page1.php
-------> [etc...]

------> [project_name]
------> [project_name]
--> [RUBY]
--> [PYTHON]

My first hurdle however is that all of my links were relative, and I had of course uploaded my site at root with my host. Root in my local dev enironment is now www, which as you can see is several folders up from where I have installed my site.

Links (e.g. <a href="/section1/page1.php">Page 1</a>) now display as: "localhost/section1/page1.php" when I believe they should in fact show as "localhost/PROJECTS/project_name/section1/page1.php".

I don't think that telling APACHE that my root directory is [project_name] is the correct approach, because then it wont work for other projects I create. I assume that I should be doing something on a project by project basis to specify it's own root directory.

I thought that I could specify root by adding an .htaccess file to the project_name folder:

From my online research I thought it would go something like:

RewriteEngine on
RewriteRule ^/$ /PROJECTS/project_name/

or perhaps

RewriteEngine on
RewriteRule ^/$ .;C/PHP/AMPSS/www/PROJECTS/project_name/

However I can't get these to work.

When the site was online, I had also coded <?php set_include_path($_SERVER['DOCUMENT_ROOT']); //Look for includes starting from ROOT location ?> at the top every page, thinking I was doing the right thing.
Now I am not so sure that I correctly understood what I was doing there. It doesn't seem to have any impact with or without it in my development environment. And I haven't been able to edit it in any way successfully to resolve my relative links issue.

Sorry for the long post.

I'm grateful for any and all feedback/assistance. Thanks in advance :)

[Resolved] - The advice below was perfect. Thank you all. :)

A tip for Chrome users:
If you are using a custom domain, like I did with .dev, either proceed the address with http://, or end it with a trailing /.
For example I now visit my dev site in Chrome by visiting projectname.dev/ (without the last / it tries to search).

At first I thought I had set up VirtualHost or hosts incorrectly, when in reality it was this Chrome behaviour that that was clouding the issue. At the time of writing, other browsers like Firefox don't share this concern.
Hope that helps someone else out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-27 19:09:52

您说得对,为每个项目配置 Apache 是前进的方向。最好使用虚拟主机。为每个项目定义一个虚拟主机,并为其指定一个域名或子域以供开发。就我而言,我使用 test.com 进行所有本地开发,并配置 project1.test.comproject2.test.com 等,以便全部我的相对路径按预期工作。为此,我还编辑我的hosts 文件,以便 DNS 查找 < code>test.com 和子项目解析到我的本地计算机。

重写 URL 是我为每个项目保留的内容,无需为特定服务器设置设置全局重写。这样我的本地开发环境就接近实际的服务器环境,并且特定于项目的重写将按预期工作。

希望有帮助

You're correct that configuring Apache for each project is the way forward. It's best done with virtual hosts. Define a virtual host for each project and give it a domain name or subdomain for development. In my case I use test.com for all my local development and configure project1.test.com, project2.test.com etc so all my relative paths work as expected. For this I also edit my hosts file so the DNS lookup for test.com and sub projects resolve to my local machine.

Rewriting URLs is something I reserve for a project-by-project basis, without setting global rewrites for a specific server setup. That way my local development environment is close to the actual server environment and project-specific rewrites will work as expected.

Hope that helps

脱离于你 2024-12-27 19:09:52

我建议使用@Tak提到的方法,但我知道预配置的WAMP包添加虚拟主机可能会出现问题,所以还有一个使用 BASE 标签位于 html 的 head 部分:

<base href="http://localhost/PROJECTS/project_name/">

I recommend using method mentioned by @Tak, but I know there might be problems with pre-configured WAMP packs to add virtual hosts, so there is also a solution to use BASE tag in head section of your html:

<base href="http://localhost/PROJECTS/project_name/">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文