Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For this solution, I have followed the following rules:</p> <ol> <li>If the user tries to load <code>/something.php</code> they should be externally redirected to <code>/something</code>.</li> <li>If the user tries to load <code>/something</code> then they should be internally redirected to <code>/something.php</code>.</li> <li>If the user passed any query string parameters to the URL then these should be preserved through the redirects.</li> <li>If the user tries to load a different file which really exists on the filesystem (a stylesheet, image etc) then this should be loaded as is.</li> </ol> <p>And here's the final set of mod_rewrite magic:</p> <pre><code>RewriteEngine on RewriteBase / ## Always use www. RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC] RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301] # Change urlpath.php to urlpath ## Only perform this rule if we're on the expected domain RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC] ## Don't perform this rule if we've already been redirected internally RewriteCond %{QUERY_STRING} !internal=1 [NC] ## Redirect the user externally to the non PHP URL RewriteRule ^(.*)\.php$ $1 [L,R=301] # if the user requests /something we need to serve the php version if it exists ## Only perform this rule if we're on the expected domain RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC] ## Perform this rule only if a file with this name does not exist RewriteCond %{REQUEST_FILENAME} !-f ## Perform this rule if the requested file doesn't end with '.php' RewriteCond %{REQUEST_FILENAME} !\.php$ [NC] ## Only perform this rule if we're not requesting the index page RewriteCond %{REQUEST_URI} !^/$ ## Finally, rewrite the URL internally, passing through the user's query string ## using the [qsa] flag along with an 'internal=1' identifier so that our first ## RewriteRule knows we've already redirected once. RewriteRule ^(.*)$ $1.php?internal=1 [L, QSA] </code></pre>
 

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