Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>RewriteCond</code> <em><a href="https://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond" rel="nofollow">test string</a></em> contains the server-variable <code>%{THE_REQUEST}</code> which takes the form:</p> <pre><code>GET /index.php HTTP/1.1 or POST /login.php HTTP/1.1 </code></pre> <p><br></p> <p>The <em>condition pattern</em> <code>^.*/index.php</code> contains a regular expression that matches anything with <code>/index.php</code> in it...</p> <p>The <code>^</code> marks the start of the string, the <code>.*</code> matches zero-or-more instances of <em>any</em> character and the <code>/index.php</code> is self-explanatory.</p> <p>So, any URL which contains <code>/index.php</code> will be matched. If the <em>condition pattern</em> ended with a <code>$</code> symbol (the end-of-string symbol), then it wouldn't match any instance of <code>%{THE_REQUEST}</code> as all instances of <code>%{THE_REQUEST}</code> contain the HTTP request type after the URL.</p> <p><br></p> <p>The <code>RewriteRule</code> is then used to do the 301 redirect as you describe, but importantly, the <a href="https://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule" rel="nofollow">RewriteRule directive</a> matches it's <em>pattern</em> against the current URL (and not against the whole request string like the RewriteCond does in this example).</p> <p>The RewriteRule <em>pattern</em> <code>^(.*)/index.php</code> captures (denoted by the brackets) everything in front of <code>/index.php</code> and then creates a 301 redirect that removes the <code>/index.php</code>.</p> <p><br></p> <p>@anubhava has pointed out that if you visit <code>website.com/index.php</code> then the RewriteRule won't work, because the URL it matches against does not include the leading <code>/</code>.</p> <p><br></p> <p>Those two lines can be rewritten as:</p> <pre><code>RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301,L,NC] </code></pre> <p>Then, if you visit <code>http://website.com/index.php</code> then the <code>(.*)</code> part of the <em>pattern</em> will match an empty string.</p> <p>Or if you visit <code>http://website.com/sub/folder/index.php</code> then the <code>(.*)</code> part of the <em>pattern</em> will match <code>sub/folder/</code>, which is then referenced in the <em>substitution</em> string by <code>$1</code>.</p> <p><br></p> <p>Remember that every redirect works by telling the browser to request a new URL, and each request gets re-processed by the htaccess, so if there are other rules that allow viewing <code>index.php</code> then that could explain why you can see some URLs containing index.php.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload