简体   繁体   English

htaccess删除结尾的斜杠,强制HTTPS,隐藏php扩展名

[英]htaccess Remove trailing slash, force HTTPS, hide php extension

I've got an SSL certificate, and I need to force https on my website. 我有一个SSL证书,并且需要在我的网站上强制使用https。 Additionally, I need to remove trailing slashes, and hide the .php extension from files. 此外,我需要删除斜杠,并从文件中隐藏.php扩展名。

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. 可以肯定的是,我们需要一些特定的请求URL,该URL会产生无限的重写循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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