htaccess redirect index.php to root (including subdomains)

左心房为你撑大大i 提交于 2019-12-28 04:50:26

问题


I'm trying to redirect index.php files to the root /, I've searched around and found several snippets of code similar to:

RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ http://www.domain.com/$1 [R=301,L] 

This however won't work for subdomains, example below:

  1. I have a subdomain: subdomain.domain.com
  2. A user visits http://subdomain.domain.com/index.php
  3. Using the htaccess above they are redirected to http://www.domain.com/ rather than http://subdomain.domain.com/

Does anyone know how to adjust the htaccess above so it will take into account the subdomain and redirect them to the appropriate location?

e.g. if a user visits http://subdomain.domain.com/index.php they will go to http://subdomain.domain.com/

Bonus Points:

Is there a htaccess that can just apply this rule to all folders?

So for any folder with an index.php they will just be redirected to it's root e.g. http://subdomain.domain.com/folder1/folder2/index.php would automatically go to http://subdomain.domain.com/folder1/folder2/


回答1:


Do this:

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



回答2:


I based the following code on @ThinkingMonkey's answer

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

It redirects URIs ending with index.php, index.htm or index.html to /. Works with subdirectories, too.

Also, notice the NC flag. It makes it case-insensitive. So it works even if the URI is in upper-case such as INDEX.PHP.




回答3:


Can be use this

RewriteRule ^index\.php/(.*)$ http://www.example.com/$1 [R=301,L]


来源:https://stackoverflow.com/questions/9369917/htaccess-redirect-index-php-to-root-including-subdomains

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