Using .htaccess, never show index.php

江枫思渺然 提交于 2019-12-11 17:48:42

问题


How can I never show index.php? For example, all requests to
site.com/index.php/controller are redirected in the browser address bar to
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

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. Here are a few...
Not Show index.php in subdirectory
remove index.php in codeigniter


回答1:


Try adding the following to your .htaccess file in the root of your domain

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

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]



回答2:


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 /(.*) (i.e. anything that followed index.php in the original request). The QSA flag appends the query string, and R makes it a hard Redirect rather than just rewriting the URL on the current request.




回答3:


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]


来源:https://stackoverflow.com/questions/8298787/using-htaccess-never-show-index-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!