skip to Main Content

I can open my site like this:

www.mysite.com

or like this:

www.mysite.com/index.php

I want to create a htaccess rule that redirects www.mysite.com/index.php to www.mysite.com. But all my attempts have other side effects. I’ve tried:

Redirect index.php    home.php
RewriteRule    ^index.php?$    home.php    [NC,L]
RewriteRule    ^index.php/?$    redirect_to_home.php    [NC,L]

But all of these mess up the original index.php call. So it does redirect but then the normal mysite.com link doesnt work anymore.

Any ideas?

2

Answers


  1. Use this redirect rule to remove /index.php from any path:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} /index.php [NC]
    RewriteCond %{REQUEST_URI} ^(.*/)index.php$ [NC]
    RewriteRule ^ %1 [L,R=301,NE]
    
    Login or Signup to reply.
  2. Could you please try following.

    RewriteEngine On
    RewriteCond  %{REQUEST_FILENAME} -f
    RewriteCond %{THE_REQUEST} index.php
    RewriteCond %{REQUEST_URI} ^(.*/)index.php$
    RewriteRule ^ %1 [R=301,L]
    

    Explanation:

    • Making RewriteEngine On to make the rules work.
    • Mentioning condition by RewriteCond to REQUEST_FILENAME which checks if mentioned file in browser is present.
    • Then checking if THE_REQUEST which has complete details of request(including URL and GET/POST method) if it has index.php in it.
    • Now checking if REQUEST_URI is having index.php in requested url, where saving everything before it to temp buffer memory to retrive its value later(basically its domain name).
    • Finally in RewriteRule to redirect complete URL with index.php to till domain name only as per requirement(R=301 is for permanent redirection on browser side).
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search