首页 > 解决方案 > 更改 PHP 收到的 URL(通过 .htaccess?)

问题描述

我正在通过从 laracasts.com 构建一个项目来学习 PHP。我正在运行一个 XAMPP 堆栈。

在项目中,我正在读取浏览器通过读取 $_SERVER['REQUEST_URI'] 发送给我的 URL。当我在浏览器中输入

http://localhost/php-learning/

我收到

/php-学习/

这是我所期望的。但是当我在浏览器中输入

http://localhost/php-learning/联系人

我收到错误 404:找不到对象。只有当我输入

http://localhost/php-learning/index.php/contact

我收到

/php-learning/index.php/contact

有人可以建议我一个可以了解这里发生的事情的地方吗?我怎样才能摆脱我的网址中的 index.php?

也许对你们中的许多人来说,这是一个非常基本的问题,但我正在学习 ;-))

亲切的问候,

休伯特

关于适应虚拟主机:

I can only find a file httpd-vhosts.conf which contains
# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "${SRVROOT}/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error.log"
    CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "${SRVROOT}/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

标签: phpurl

解决方案


验证您的 Nginx/Apache vhosts 配置,在那里您会找到一条规则,该规则声明在 / 路径中包含“index.php”。只需在 index.php 之前添加一个斜杠,它就会看起来像 /index.php。问题是当您不在 index.php 之前添加 / 时,它会尝试在您的子目录路径中找到单独的 index.php。因此,如果您在它之前添加 /,那么它将始终使用根 index.php 文件。


推荐阅读