I'm trying to write some mod rewrite code to redirect pages on a site to always use www and this has been working fine:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/1ドル [R=301,L]
However, I now want to make sure this doesn't happen on my dev environment, which uses the .local
domain extension...
So I changed it to this:
RewriteCond %{HTTP_HOST} !\.local$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/1ドル [R=301,L]
Does that seem correct?
As I understand it, it states: "If it doesn't END with .local
AND it doesn't START with www.
THEN do the RewriteRule"?
1 Answer 1
Yes! That is indeed correct. From the docs for RewriteCond
:
'
ornext|OR
' (or next condition)Use this to combine rule conditions with a local OR instead of the implicit AND.
The [AND]
flag is the default behaviour of combining multiple conditions expressions together.
As for the rule itself, I'd suggest using %{REQUEST_URI}
instead of 1ドル
in rewrites:
RewriteCond %{HTTP_HOST} !\.local$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
-
\$\begingroup\$ Thanks. Is there a reason you suggest using
%{REQUEST_URI}
over1ドル
and why is the^
portion of that last rule needed - seems pointless now? \$\endgroup\$Brett– Brett2018年07月26日 20:45:54 +00:00Commented Jul 26, 2018 at 20:45 -
\$\begingroup\$ Not much if it is being used from
.htaccess
, but at VHost level or server level config,1ドル
will contain a leading/
, causing rewrite to<host>//<path>
(double/
in URL) \$\endgroup\$hjpotter92– hjpotter922018年07月27日 07:40:30 +00:00Commented Jul 27, 2018 at 7:40 -
\$\begingroup\$ Gotcha - cheers! :) \$\endgroup\$Brett– Brett2018年07月27日 13:18:06 +00:00Commented Jul 27, 2018 at 13:18
Does that seem correct?
Does it work? Did you test it? \$\endgroup\$