简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM