What does $1 in .htaccess file mean?

by priscilla.langworth , in category: SEO , 4 months ago

What does $1 in .htaccess file mean?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by declan_ritchie , 4 months ago


In .htaccess file, the "$1" symbolizes a backreference to the match of the first capturing group in a regular expression.

When you use regular expressions in .htaccess files (which are used by Apache web servers to modify server configurations per directory), you can define patterns to match specific URLs or file paths. By enclosing parts of the pattern in parentheses, you create capturing groups that can be referenced with "$1" (for the first group), "$2" (for the second group), and so on.

For example, if you have the following rewrite rule in your .htaccess file:

RewriteRule ^([a-z]+)/?$ index.php?page=$1 [NC,L]

Here, the pattern "([a-z]+)" captures one or more lowercase letters. The "$1" in the replacement string refers to the captured value, which will be passed as the "page" parameter to the "index.php" script.

So, if a user visits "example.com/about", the rewritten URL will be "example.com/index.php?page=about" thanks to the "$1" backreference.