0

The goal here is if the URL has got a trailing slash, like h2tps://domain.com/page/, redirect to h2tps://domain.com/page

h2tp(s)://(www.)domaine.com/page/ => h2tps://domain.com/page

It should work only in prod environment, not dev, it's why the condition must apply only with domain.com. domain.com is set as a variable to have an easy configuration in more complex environments.

Actually, this is my .htaccess :

RewriteEngine on
RewriteBase /

RewriteRule .* - [E=DOMAIN:domain.com]

# Remove ending / AND rewrite with https and without www in a single redirection
RewriteCond expr "%{HTTP_HOST} -strmatch '(www\.)?%{ENV:DOMAIN}'" [NC]
RewriteCond %{REQUEST_URI} /public/(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) https://%{ENV:DOMAIN}/%1 [R=301,L]

I've tried with -strmatch and = in the expression. The fact is that h2tps://domain.com/page/ redirects, but not h2tps://www.domain.om/page/, as if the optional (www.)? were ignored.

I need help on this one please!

Cyril
  • 177
  • 1
  • 7
  • 1
    `-strmatch` doesn't do a regex match, it is string matching with support for a few wildcards (*, ?, [] - but with different meaning than in regex.) – CBroe Sep 22 '22 at 11:08
  • Where is your `.htaccess` file located? You reference a `/public/...` URL-path in the second _condition_, but this is not present in your example URLs? (Are you perhaps rewriting the request from a parent directory to `/public`?) The above rule would not redirect anything as written - if you are seeing a redirect then this is most likely a cached redirect (301 - permanent - redirects are cached persistently by the browser and possibly intermediary caches). – MrWhite Sep 22 '22 at 16:30
  • It's ok for public/, it's the location of the actual .htaccess (there's another one in root website that redirect to public/. I've find the solution, i will post it in a few hours. Thanks! – Cyril Sep 22 '22 at 17:51

1 Answers1

0

So, Thanks to CBroe first comment, I come to this solution :

RewriteCond expr "%{HTTP_HOST} -strcmatch '*%{ENV:DOMAIN}'"

the * character is a wildcard that means any characters, including no character. 'www.domain.com' and 'domain.com' are matching.

Cyril
  • 177
  • 1
  • 7