Force https, www and bar at the end of the URL with . htaccess

Asked

Viewed 530 times

-1

Good afternoon Personal.

I’m trying to get . htaccess to force three things. Always access with https, www before the domain and a bar at the end of the URL.

Always following this pattern: https://www.site.com.br/pagina/

Before I used only:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

To redirect any URL to my index.php. If I accessed www.site.com.br/index.php it would not directly access the file, which allowed me to redirect to a 404 error page.

Researching around here I arrived at a solution that works in parts:

RewriteEngine On
RewriteCond %{REQUEST_URI}  !\.(php|html?|jpg|gif|css|js)$
RewriteRule ^(.*)([^/])$ https://www.site.com.br/$1$2/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Always access it the way I want. The problem is that when I access, for example, wwww.site.com.br/index.php it allows direct access to the file and I cannot redirect to a 404 error page.

I tried to remove the PHP extension from the second line and even the entire second line. But in such cases, although it redirects wwww.site.com.br/index.php to wwww.site.com.br/index.php/ as I would like, it also redirects any page ( /home/ or /page/, for example ) to /index.php/

Any idea what it might be?

Thanks in advance.

  • This problem has been dealt with numerous times here on the site, use the search at the top to locate the one that best suits your case (it is ceerteza that has several that meet, just need to see which you find more understandable to apply)

  • Bacco, I even researched here but, were the cases I found that make me arrive at the example I have in the question. But the problem was to add the three amendments I wanted without making a mistake. I ended up finding an answer, I will edit the question with the solution I found with the help of a friend.

  • Darlei, I may be wrong but I don’t see it as duplicate. Anyway, thank you.

1 answer

2


With some help I managed to come up with a solution to what I wanted in the question. I’m answering here in case anyone has a similar need.

RewriteEngine on

# Adicionar barra no final da URL, caso não tenha.
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

# Forçar HTTPS e WWW 
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off  
RewriteRule ^(.*)$ https://www.acwebs.com.br/$1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.