使用 Apache2 和 Yii 进行本地域模拟
我正在尝试将 http://localhost/website
转换为 http://website.loc
,但我无法做到这一点。这是我可以做的:
我编辑了 /etc/hosts (我在 Ubuntu 上),将 127.0.0.1 localhost
更改为 127.0.0.1 localhost website.loc
并保存更改
我在 /etc/apache2/sites-available
中创建了一个名为 website
的新文件,其中包含以下内容:
<virtualhost website.loc>
ServerName website.loc
DocumentRoot /home/myuser/projects/website/
<directory /home/myuser/projects/website/>
AllowOverride all
Options Indexes FollowSymLinks MultiViews
Order allow,deny
allow from all
</directory>
</virtualhost>
并且我创建了一个指向启用站点的软链接以启用此功能。之后,我重新启动了 Apache。 顺便说一句,我使用 Yii 框架,任何对 / 的请求都会重定向到 /index.php,因此查询中不需要 index.php。
因此,当我将 website.loc/ 写入 chrome 时,它会将我移至 http://website.loc/site/login (登录索引页面,即使我以 localhost 身份登录,这也几乎是预期的,因为站点 url“更改”为 website.loc,因此 cookie 不共享),但内容是:
Not Found
The requested URL /website/index.php was not found on this server.
Apache/2.2.16 (Ubuntu) Server at website.loc Port 80
我在做什么吗有事吗?预先感谢,伙伴们
编辑:这都是关于 /home/myuser/projects/website 内的 .htaccess 的。它的 RewriteBase 指向 /website。将其更改为 / ,效果非常好。感谢@Chux 提醒我检查 .htaccess!
I'm trying to convert http://localhost/website
to http://website.loc
, but I'm not able to do that. Here's what I could do:
I edited /etc/hosts (I'm on Ubuntu) by changing 127.0.0.1 localhost
to 127.0.0.1 localhost website.loc
and saved changes
I created a new file named website
inside /etc/apache2/sites-available
with this content:
<virtualhost website.loc>
ServerName website.loc
DocumentRoot /home/myuser/projects/website/
<directory /home/myuser/projects/website/>
AllowOverride all
Options Indexes FollowSymLinks MultiViews
Order allow,deny
allow from all
</directory>
</virtualhost>
And I created a softlink to sites-enabled to enable this. After that, I restarted Apache.
By the way, I am using the Yii framework with any request to / redirected to /index.php, so index.php is not needed in the query.
So, when I write website.loc/ into chrome, it moves me to http://website.loc/site/login (the login index page, that's almost expected even if I was logged in as localhost, because the site url "changed" to website.loc, so the cookies are not shared), but the content is:
Not Found
The requested URL /website/index.php was not found on this server.
Apache/2.2.16 (Ubuntu) Server at website.loc Port 80
Am I doing something wrong? Thanks in advance, mates
Edit: It was all about the .htaccess inside /home/myuser/projects/website. It's RewriteBase was pointing to /website. Changing this to / and it worked like charm. Thanks @Chux for reminding me to check the .htaccess!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,http://website.loc/index.php,检查是否有效。如果有效,则意味着您需要在网站根文件夹中创建 .htaccess 以启用该路由格式
First, http://website.loc/index.php, check if that work. If that work, means that you need to create an .htaccess in your website root folder to enable that route format