URL重写我不想在url中显示index.php

问题描述 投票:0回答:2

我正在

上运行我的 php 应用程序

http://localhost:8085/

我的目录结构是这样的: C:\wamp\www\网站

我已将 .htaccess 文件放在网站文件夹中。

<IfModule mod_rewrite.c>
#Turn on the RewriteEngine
RewriteEngine On
RewriteBase /website/
#Rules
RewriteRule ^(.*)$ index.php
</IfModule>

当我点击index.php时,它会将我带到http://localhost:8085/主页,这是错误的,我想要访问http://localhost:8085/website

请指导我

php .htaccess url-rewriting
2个回答
2
投票

您需要在index.php之前添加

website
,例如,

RewriteRule ^(.*)$ /website/index.php

已更新,尝试完整代码,

RewriteEngine On
RewriteBase /website/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /website/index.php [L] 

注意:您需要在网站中使用的每个链接中使用

http://localhost:8085/website/
作为基本 URL,否则它将重定向到
http://localhost:8085/
,例如,

<a href="http://localhost:8085/website/index.php">Home page</a>

或者,请勿在任何

/
之前使用
href
,并按原样使用您的文件名,例如,

<a href="index.php">Home page</a>

如果您使用

<a href="/index.php">Home page</a>
,它将重定向到URL
http://localhost:8085/

的索引页面

您可以制作一个虚拟主机来直接访问它https://httpd.apache.org/docs/current/vhosts/name-based.html


0
投票

您面临的问题似乎与 .htaccess 文件中的 RewriteBase 指令有关。 RewriteBase 指令指定用于具有相对替换 URL 的每个目录 (htaccess) RewriteRule 指令的基本 URL 路径。

要实现在单击 index.php 时重定向到 http://localhost:8085/website 而不是 http://localhost:8085/ 的目标,您可以按如下方式更新 .htaccess 文件:

<IfModule mod_rewrite.c>
    # Turn on the RewriteEngine
    RewriteEngine On
    # Set the base directory
    RewriteBase /website/
    # Specify the rewrite conditions
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Rewrite the URL to index.php
    RewriteRule ^(.*)$ index.php [L]
</IfModule>

在此更新版本中,我添加了两个 RewriteCond 指令来检查请求的文件或目录是否不存在。这可以防止在文件或目录实际存在时应用规则,这应该有助于解决您面临的问题。

确保清除浏览器缓存或尝试使用其他浏览器来测试更改。此外,请确保在您的 Apache 配置中启用 mod_rewrite。

如果您仍然遇到问题,您可能需要检查 Apache 错误日志中是否有任何相关消息,以便更深入地了解问题。

© www.soinside.com 2019 - 2024. All rights reserved.