remove index.php url rewriting .htaccess

15,684

Solution 1

It looks like you have your htaccess file in your document root on your server, and in the mysite directory on localhost. Since the location of the htaccess file is pretty important on how it routes URIs, you need to make it indifferent to the location of the file. You can do this by extracting the path info from your condition instead of the URI that's passed into the rule to match against:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

The %{THE_REQUEST} variable is the first line of the actual HTTP request, which looks something like:

GET /path/index.php HTTP/1.1

The pattern first matches any number of possible METHODS (GET, POST, HEAD, etc), then it creates a grouping of the URI path that's before the index.php, then ends the matching, since we don't really care what's after the index.php.

Solution 2

try this

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

Solution 3

If your site is not at the root of the server (which it's not on your localhost), you will need to add a RewriteBase directive: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

You need to add the line:

RewriteBase /mysite/

above the current lines in the htaccess on your localhost

Share:
15,684
Mohammad Faisal
Author by

Mohammad Faisal

@faisal6621 facebook linkedin Er. Mohammad Faisal (Blogger) Er. Mohammad Faisal (Wordpress) If you are looking for something to vote on, I'm looking for votes (ideally upvotes 🤪) on [my questions/answers with zero][1]...

Updated on June 11, 2022

Comments

  • Mohammad Faisal
    Mohammad Faisal almost 2 years

    I'd written this code for 301 redirect

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

    It is working well in case if I do visit my site as http://mysite.com/index.php, it redirects me to http://mysite.com

    But on my localhost if I try to visit index.php as localhost/mysite/index.php it redirects me to localhost.

    How could I solve this problem? Is the code written above is correct?