2010-12-06 158 views
32

我有一个问题,谷歌已经索引了一些页面的URL错误。htaccess从url中删除index.php

的URL他们索引是:

http://www.example.com/index.php/section1/section2 

我需要它重定向到:

http://www.example.com/section1/section2 

的.htaccess是不是我的专长,所以任何帮助,将不胜感激。

在此先感谢。

回答

43

这是从URL中隐藏index.php的基本规则。把这个放在你的根目录.htaccess文件中。

mod_rewrite必须启用PHP,这将适用于高于5.2.6的PHP版本。

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

非常感谢,还有一个问题 - 我如何替换下划线的连字符,即 http://www.mydomain.com/section-1/section-2 至 http://www.mydomain.com/section_1/section_2 – Nick 2010-12-06 10:04:54

+6

如果您真的在为SEO工作,无需将连字符转换为下划线。人们正在做SEO的逆向。连字符是谷歌友好。 – 2010-12-06 10:12:08

+13

这不会删除index.php,而是添加它! – Calmarius 2014-05-06 08:17:24

15
RewriteEngine On 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?) 
RewriteRule^/%1 [R=301,L] 
28

从URL中移除index.php,以及访问者重定向到非index.php文件版本的页面:

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

这将干净重定向/index.php/myblog简单/myblog

使用301重定向将保留Google搜索引擎排名。

4

执行以下步骤

确保主机/你的电脑的mod_rewrite模块处于活动状态。如果没有激活,然后尝试以某种方式激活,请打开httpd.conf文件。你可以在phpinfo.php中查看它以找出答案。

改变这个设置:

#LoadModule rewrite_module modules/mod_rewrite.so 

是,重启WAMP

LoadModule rewrite_module modules/mod_rewrite.so 

2.然后去.htaccess文件,并试图修改为:

RewriteEngine On 

RewriteBase/

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA] 

如果上面不行,试试这个:

RewriteEngine on 

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# otherwise forward it to index.php 
RewriteRule . index.php 

移动.htaccess文件根目录,其中是的index.php那里。

www OR root folder 
- index.php 
- .htaccess 
3

假设存在的网址是

http://example.com/index.php/foo/bar 

,我们希望将其转换成

http://example.com/foo/bar 

您可以使用以下规则:

RewriteEngine on 
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar" 
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC] 
RewriteRule^/%1 [NE,L,R] 
#2)internally map "/foo/bar" to "/index.php/foo/bar" 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ /index.php/$1 [L] 

在SPEP #1我们首先匹配请求字符串并捕获/index.php/后的所有内容,并将捕获的值保存在%1 var。然后,我们将浏览器发送到一个新的URL。 #2在内部处理请求。当浏览器到达/foo/bar时,#2rule会将新网址重写为原始位置。