tristanojbacon
tristanojbacon

Reputation: 456

htaccess Remove trailing slash, force HTTPS, hide php extension

I've got an SSL certificate, and I need to force https on my website. Additionally, I need to remove trailing slashes, and hide the .php extension from files.

This is what I have so far, but it's causing too many redirects:

RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ https://hellorufus.com/$1 [R,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://hellorufus.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# 404 Error Template
ErrorDocument 404 /404.html

I've found a number of similar questions, but when I try to piece them together for my own needs, I end up with too many redirects again!

Upvotes: 0

Views: 766

Answers (1)

arkascha
arkascha

Reputation: 42935

This is a version with a few corrections that appear plausible:

RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=302,L,QSA]

# Redirect external .php requests to extensionless url
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]

# 404 Error Template
ErrorDocument 404 /404.html

Not sure if that already solves your issue. To be certain we would need some specific request URL that generates an endless rewrite loop.

Upvotes: 1

Related Questions