简体   繁体   English

使用.htaccess,从不显示index.php

[英]Using .htaccess, never show index.php

How can I never show index.php? 我怎么不能显示index.php? For example, all requests to 例如,所有对
site.com/index.php/controller are redirected in the browser address bar to 在浏览器地址栏中将site.com/index.php/controller重定向到
site.com/controller ? site.com/controller

My current .htaccess removes index.php but when a user directly types site.com/index.php/controller they are still shown that address in the address bar as opposed to site.com/controller 我当前的.htaccess删除了index.php,但是当用户直接键入site.com/index.php/controller时,它们仍然在地址栏中显示该地址,而不是site.com/controller

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

NOTE: Before flaming, I have checked lots of .htaccess threads that solve redirecting index.php but I haven't found one to never show index.php. 注意:在燃烧之前,我检查了许多.htaccess线程,这些线程可以解决重定向index.php的问题,但是我还没有发现一个永远不会显示index.php的线程。 Here are a few... 这里有一些...
Not Show index.php in subdirectory 在子目录中不显示index.php
remove index.php in codeigniter 在codeigniter中删除index.php

Try: 尝试:

RewriteEngine On
RewriteBase /

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

I have not tested this, but as I understand it, it will take an URL with index.php as the filename (regardless of anything following it) and redirect it to /(.*) (ie anything that followed index.php in the original request). 我尚未对此进行测试,但据我了解,它将以index.php作为文件名的URL(无论后面是否有任何内容)并将其重定向到/(。*)(即,原始请求)。 The QSA flag appends the query string, and R makes it a hard Redirect rather than just rewriting the URL on the current request. QSA标志附加查询字符串,R使它成为硬重定向,而不仅仅是重写当前请求上的URL。

For anyone looking for a more generalized solution: 对于正在寻求更通用解决方案的任何人:

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]

Try adding the following to your .htaccess file in the root of your domain 尝试将以下内容添加到域根目录中的.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]

If you need this to work for any path_info use the rule below instead 如果您需要将它用于任何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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM