I need a quick review of my updated .htaccess file please:
RewriteEngine On
# Redirects errors to 404 page
ErrorDocument 404 /404.php
# Redirect index.php to site root
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC]
RewriteRule ^index.php$ http://www.mysite.com/ [R=301,L]
# Unless directory remove slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
# Remove .php
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/.]+)$ $1.php [L]
It’s fairly straight forward.
The first is a 404 redirect.
The second redirects http://www.mysite.com/index.php to http://www.mysite.com
The third removes slashes.
The fourth removes the .php extension.
The fifth resolves php.
Could someone cast an eye over this and give my code a quick sanity check please.
The below may make more sense at the top as you are redirecting the user and they will go through all of the .htaccess file again.
What you’re actually doing here is if any of these are
trueyou are telling the browser to go and load the website again from a different url “[R=301]“.Your other redirects are in a sensible order, if not a directory drop the slash, if a php drop the .php then resolve anything without a slash.
Note this will result in a quite a few redirects if a user visits your website at certain url’s for example
mysite.com/cake.php/(which doesn’t exist)mysite.com/cake.php/=>mysite.com/cake.phpmysite.com/cake.php=>mysite.com/cakemysite.com/cake=>http://www.mysite.com/cakehttp://www.mysite.com/cakeresolves tohttp://www.mysite.com/cake.php– doesn’t exist404=>http://www.mysite.com/404.phphttp://www.mysite.com/404.php=>http://www.mysite.com/404Though in summary it does what you are looking for!