.htaccess rewrite rules for multiple cases

I don’t have an Apache server in front of me, but does this work for the last three?

# Look for URL.php as a file
RewriteCond %{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [L]

# Look for URL as a directory
RewriteCond %{REQUEST_URI} -d
RewriteRule ^(.*)$ /$1/index.php [L]

# If the URL doesn't exist on disk in another way that Apache can
# figure out, forward it to index.hp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA,L]

EDIT: the following works well:

RewriteEngine On

# http:// -> https://
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# -> https://example.com/abc123.php if that file exists
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [END]

# -> index.php if /abc123.php and /abc123/ don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [END]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top