1

I have the following statements in an .htaccess file

RewriteCond %{HTTP_HOST} ^myOldDomain\.com$ [NC]
RewriteRule  ^(.*)$ https://myNewDomaink.com/$1 [R=301,L]

It works fine. I basically found some sample code and modified it to my specific purpose. What I don't quite understand is:

Why does $1 refer to the the portion of the supplied url after the hostname - where is the documentation for this? There is no backreference in the RewriteCond.

Scott
  • 394

2 Answers2

1

$1 refers to everything the .*-regex matched to. In most cases this will be everything after the slash. You can test these queries on sites like Rubular.com and the numbers of the matches that are shown there, those are the numbers you can use as variables. You'll have to wrap those 'groups' in brackets to use them as variables.

1

After further reading and experimenting, it looks like the $1 is referencing the backreference from the RewriteRule. Since Rewrite rules and conditions default to the URI string, the Rewrite rule is saying:

Take the URI string and add it to the end of https://myNewDomainurl.com/

Scott
  • 394