Matching a regex containing variable environment in RewriteCond of .htaccess

132 Views Asked by At

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!

1

There are 1 best solutions below

0
Cyril On

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.