Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

apache - htaccess redirect index.php to root (including subdomains)

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/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...