使用.htaccess,永远不要显示index.php

时间:2011-11-28 16:16:43

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

我怎么能永远不显示index.php?例如,所有请求到 site.com/index.php/controller 会在浏览器地址栏中重定向到
 的 site.com/controller

我当前的.htaccess删除了index.php,但是当用户直接输入 site.com/index.php/controller 时,他们仍会在地址栏中显示该地址,而不是网站.com / controller

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

注意:在燃烧之前,我已经检查了很多.htaccess线程来解决重定向index.php但是我没有找到一个永远不会显示index.php。这里有几个...
Not Show index.php in subdirectory
remove index.php in codeigniter

3 个答案:

答案 0 :(得分:0)

尝试将以下内容添加到域根目录中的.htaccess文件

RewriteEngine On
RewriteBase /

#if you get a request for this /index.php/controller
RewriteCond %{REQUEST_URI} ^/index.php/controller$
#redirect to  just controller
RewriteRule . controller     [R=301,L]

如果您需要这个适用于任何path_info,请使用以下规则

RewriteEngine On
RewriteBase /

#if you get a request for this /index.php(any-path)
RewriteCond %{REQUEST_URI} ^/index.php/(.+)$
#redirect to  any-path
RewriteRule . %1     [R=301,L]

答案 1 :(得分:0)

尝试:

RewriteEngine On
RewriteBase /

RewriteRule ^index.php(.*)% ^/$1 [QSA,R]

我没有对此进行过测试,但据我了解,它将使用带有index.php的URL作为文件名(无论后面是什么)并将其重定向到/(。*)(即跟随索引的任何内容)。 php在原始请求中)。 QSA标志附加查询字符串,R使其成为硬重定向,而不仅仅是在当前请求上重写URL。

答案 2 :(得分:0)

对于寻求更通用解决方案的任何人:

Options +FollowSymLinks -MultiViews

#Disallow listing contents of subfolders
Options All -Indexes

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If index or index.php requested, strip and redirect
RewriteCond %{THE_REQUEST} index(\\.php)?
RewriteRule ^index(\\.php)?$ http://yourdomain.com/ [R=301,L]

## Part 1
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## Part 2 - handle incoming that lacks extension (except for existing folders)
## To redirect /dir/foo to /dir/foo.php, (skip for admin or user dirs)
RewriteRule ^(admin|user)($|/) - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]