如何从 URL 中删除 index.php

[ad_1]

默认情况下,WordPress 和 Codeigniter 等 PHP 平台将 index.php 附加到网站 URL 的末尾,这并不是真正必需的。 以下是使用 .htaccess 从 URL 中删除 index.php 的步骤 Apache 网络服务器。 您可以使用这些步骤从 WordPress、Codeigniter 和其他 PHP 网站中删除 index.php。

如何从 URL 中删除 index.php

我们基本上会将 index.php 重定向到没有它的 URL。 为此,我们将需要 mod_rewrite Apache 模块。

在继续之前,请确保您已启用 mod_rewrite Apache 网络服务器。 以下是启用 mod_rewrite 的步骤 Apache. 最后,您将为您的网站创建一个 .htaccess 文件。

1.打开.htaccess文件

打开终端并运行以下命令打开 .htaccess 文件。 我们使用了 .htaccess 文件的默认文件路径。 您可以根据您的要求更改它。

$ sudo vi /var/www/html/.htaccess

奖励阅读:如何安装 Fail2Ban Apache

2. 从 URL 中删除 index.php

在 .htaccess 文件中添加以下几行。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

让我们看看上面的每一行。 如果尚未启用,第一行启用 mod_rewrite。

第二行匹配非文件的 URL。 第三行匹配非目录的 URL。 所以我们希望匹配既不是文件也不是目录的 URL。

最后一行的意思是,如果它之前的两个 RewriteCond 语句匹配一个 URL,那么它将被重定向到没有 index.php 的 URL。

奖励阅读:如何安装 memcached Apache

3.重启 Apache 网络服务器

重新开始 Apache 应用更改的服务器

$ sudo systemctl restart httpd

现在,如果您打开浏览器并访问 https://your_domain_or_IP/index.php,您将被重定向到 https://your_domain_or_IP。 将 your_domain_or_IP 替换为您的域或 IP 地址。 同样,如果您访问像 https://your_domain_or_IP/index.php/folder1/folder2 这样的 URL,您将被重定向到 https://your_domain_or_IP/folder1/folder2

[ad_2]

Related Posts