Forum Moderators: phranque
if you are using mod_rewrite anywhere, you should avoid using Redirect(Match) everywhere
^(.*)Bite your tongue ;) (Yes, I do realize that Apache docs use this form constantly. That doesn't mean it's a good idea.)
^([^_\-]+)[_-]print(er)?\.php
or
^/([^_-]+)[_\-]print(er)?\.php
meaning “start capturing from the beginning, but stop as soon as you hit a - or _” https://example.com/\1.php ^(.+)[_\-]print(er)?\.php
or
^/(.+)[_-]print(er)?\.php
which is fractionally less efficient, though not it ways you are ever likely to notice. The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.
do not mix directives from different modules
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to redirect one URL to another. This kind of simple redirection of one URL, or a class of URLs, to somewhere else, should be accomplished using these directives rather than RewriteRule.
the precise reason has changed since
So I really need to hear about this new reason, if you can spare the time.
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first
It is the server configuration which decides whether mod_alias or mod_rewrite directives are processed first, and you cannot control that by ordering the directives in your .htaccess file. In other words, the server will process all mod_alias directives first, followed by all mod_rewrite directives, or vice-versa, depending on how it's configured. So in order to enforce order of execution while using advanced features like "check requested host name" and "check for file exists" as you have done, you'll have to use mod_rewrite only, because mod_alias does not support those features.
tested your example and got a single 301 in the logs.
The Redirect includes the www and would deal with the canonical issue.
I do have a standard canonical Rewrite as well, if it executed first there is no sign of it.
if you are using mod_rewrite anywhere, you should avoid using Redirect(Match) everywhere
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first
what version of apache are you using?
# Canonical & Encryption
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
# Redirect specific files
RedirectMatch 301 (.*)-print\.php$ https://www.example.com/$1.php
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to redirect one URL to another. This kind of simple redirection of one URL, or a class of URLs, to somewhere else, should be accomplished using these directives rather than RewriteRule.
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first
if you are using mod_rewrite anywhere, you should avoid using Redirect(Match) everywhere