【问题标题】:Rewrite the exact url /index.php to /将确切的 url /index.php 重写为 /
【发布时间】:2017-04-21 21:01:08
【问题描述】:

我正在尝试重定向或重写 URL,但我无法正确处理。

我要重定向/重写的网址是:

http://www.example.com/index.php -> http://www.example.com/

而且只有上面的 exact ...index.php URL。没有查询字符串等。

基本上只是在开始/索引页面上删除 index.php。请赐教。

编辑

在使用了六个标签(我是新来的)之后,我最终删除了 apache 标签,以便澄清:我正在使用 apache。

【问题讨论】:

  • 我是白痴,还是这不会自动发生?对于这个 exact 示例?与大多数服务器一样,加载网站时默认搜索的文件是index.*
  • DirectoryIndex 有效,如果我只是去 /index.php 将被加载,但我也可以访问 index.php。

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

小技巧

在你的 index.php 中添加这个

<?php
    if($_SERVER['REQUEST_URI']=="/index.php")
        header("location:http://www.example.com");
?>

发生的情况是 $_SERVER['REQUEST_URI'] 在 URL BAR 中返回您当前的 url,因此如果它是 /index.php 将其重定向到您的域而不使用 /index.php

干杯!!

【讨论】:

  • 感谢您的回答,但我不想涉足 index.php 文件,因为我刚刚安装了这个(马马虎虎)代码。
【解决方案2】:

取决于您使用的服务器:

对于nginx,请检查以下网址: http://nginx.org/en/docs/http/ngx_http_index_module.html

对于 apache 查找 DirectoryIndex。你需要这样的东西:

DirectoryIndex index.php index.phtml index.html index.htm

根据 cmets 更新答案并更新问题

试试这个:在你的根目录中创建一个 .htaccess 文件,内容如下:

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 

【讨论】:

  • 很抱歉没有指定服务器(我的问题现在已编辑),我正在使用 apache。 DirectoryIndex 工作,如果我只是去 /index.php 将被加载,但我也可以访问 index.php。
  • 根据您的评论更新了我的答案。确保为您的 apache 启用了 mod_rewrite。
  • 感谢您的回答。但是,它完全不起作用。像:example.com/index.php?a=b 这样的 URL 将被重写为 example.com/?a=b 我只想重写确切的 /index.php(不包括查询字符串等)URL。
猜你喜欢
  • 2013-06-25
  • 2022-08-19
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
相关资源
最近更新 更多