Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $1 vs %1 in .htaccess

What is the difference between %1 and $1 in .htaccess?

For example,

    #  to remove www       RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com [NC]      RewriteRule .* http://mydomain.com/%1 [R=301,L]         #    versus     #    RewriteRule .* http://mydomain.com/$1 [R=301,L] 

I have been using Dave Child's .htaccess cheat sheet and Jackol's .htaccess cheat sheet as well as the Apache mod_rewrite docs but additional help would be great.

like image 656
csi Avatar asked Jul 11 '11 18:07

csi


People also ask

What is Rewriteengine on htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is mod_rewrite used for?

mod_rewrite lets you create all sorts of rules for manipulating URLs. For example, you can insert values pulled from the requested URL into the new URL, letting you rewrite URLs dynamically.

What is mod_rewrite?

mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. Incoming URLs are checked against a series of rules. The rules contain a regular expression to detect a particular pattern.

What means in htaccess?

htaccess is a plain text configuration file for the Apache HTTP Server that allows administrators to specify options for directories where web content is served. The initial "." in . htaccess means that the file will be invisible on Unix-like systems in directory listings except with the "ls -a" command.


1 Answers

%1 Refers to a pattern matched in a RewriteCond condition, while $1 refers to a pattern matched inside a RewriteRule.

More generically, use %n to refer to the numbered matches from RewriteCond condition regex patterns, and use $n to refer to numbered matches from RewriteRule regex patterns.

like image 55
Michael Berkowski Avatar answered Sep 22 '22 05:09

Michael Berkowski