2017-04-21 91 views
0

我正在尝试重定向或重写一个URL,但我无法正确理解它。将网址/index.php改写为/

我想重定向的URL /改写为:

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

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

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

编辑

我结束了使用六种标记后去除apache的标签(我是新来的),以便澄清:我使用的Apache。

+0

我是白痴,还是不会自动发生?对于这个*确切*例子?与大多数服务器一样,加载网站时默认搜索的文件是'index。*'。 – Nytrix

+0

DirectoryIndex的作品,如果我只是去/ index.php将被加载,但我也可以访问index.php。 – saturnusringar

回答

0

取决于你所使用的服务器:

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

apache查找DirectoryIndex的。你需要这样的东西:根据意见和更新问题

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

更新应答试试这个:

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

对不起,没有指定服务器(我的问题现在编辑),我使用Apache。 DirectoryIndex的作品,如果我只是去/ index.php将被加载,但我也可以访问index.php。 – saturnusringar

+0

根据您的评论更新我的答案。确保你的apache启用了mod_rewrite。 – codingb

+0

感谢您的回答。尽管如此,它完全行不通。 URL: http://www.example.com/index.php?a=b将被重写为http://www.example.com/?a=b 我只想重写确切的/ index.php(不含查询字符串等)URL。 – saturnusringar

0
:在你的ROOT目录下面的内容创建一个.htaccess文件

小窍门

将此内容添加到您的index.php

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

发生的事情是$ _ SERVER [“REQUEST_URI”]在地址栏返回您目前的网址,所以如果它的index.php重定向到您的域,而无需的index.php

干杯!

+0

感谢您的答案,但我宁愿不涉足index.php文件,因为我刚刚安装了这个(马马虎虎)的代码。 – saturnusringar

相关问题