从网站URL中删除index.php

时间:2009-05-25 01:22:18

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

我正在使用Kohana框架(但我认为这与此问题无关)并且可以像这样访问页面

http://www.example.com/articles/
http://www.example.com/index.php/articles/

现在,根据经验,我通常会尝试调整我的.htaccess,只允许一种方式进入页面,并默默地重定向其他常用方法。

基本上,在上面的第一个URL中,地址实际上是内部重定向到第二个示例。

我想要做的是强制第二种类型的任何URL转换为第一种类型的URL。我并不经常对.htaccess充满信心,我的第一次尝试就是抛出一些意想不到的结果(有时会像无尽的循环一样)

以下是我提出的内容

RewriteRule ^index\.php/(.*) $1 [NC,L,R=301]

任何人都可以告诉我我做错了什么,如果你也遇到过这个问题,你是怎么解决的?

修改

我决定发布我的整个.htaccess,以便检查我的所有重定向。

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /~toberua/


# file not found page
    ErrorDocument 404 /404/
    ErrorDocument 403 /403/

# get people out of my directories
    Options -Indexes

# default page to load
    DirectoryIndex index.php

# add trailing slash if missing
    RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]

# redirect /favicon.ico requests
     RewriteCond %{REQUEST_URI} !^/images/layout/favicon\.ico [NC]
     RewriteCond %{REQUEST_URI} favicon\.(gif|ico|png|jpe?g) [NC]
     RewriteRule (.*) images/layout/favicon.ico [R=301,L]

# send /home back to TLD
     RewriteRule home/ $1  [NC,R=301,L]

# ensure there is no /index.php in the address bar
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
    RewriteRule ^(.*)index\.php$ $1 [R=301,L] # this was my attempt to stop /dir/index.php and make it simply /dir/

    RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301]

# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]

1 个答案:

答案 0 :(得分:3)

试试这个:

RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301]

您未处理的是所有请求都需要重写,当您进行重写时,它会生成一个子请求 - 也可能会被重写。所以你最后重写/文章到/index.php/articles,然后在子问题中,你将/index.php/articles重写为/ articles并生成301重定向的新请求,打开和关闭。添加NS标志将使此规则不对子请求进行操作,我认为这应该解决您的问题,除非您还在/ articles - >上执行301操作。 /index.php/articles重写(但那会很疯狂)。