Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does $1 in .htaccess file mean?

I am trying to understand the meaning of this line in the .htaccess file

 RewriteRule ([a-z0-9/-]+).html $1.php [NC,L,QSA]

basically what does $1.php ? what file in the server

if we have home.html where this gonna redirect to? home.php?

like image 287
user3150060 Avatar asked Jul 23 '14 22:07

user3150060


People also ask

What is $1 rewrite rule?

Yes. In the RewriteCond , it's basically saying that it'll run the rewrite as long as $1 doesn't equal on of the files listed to the right of the condition.

What does QSA mean in htaccess?

The QSA flag means to append an existing query string after the URI has been rewritten.

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.

What is a htaccess file?

The .htaccess (short for 'hypertext access') file is a distributed server configuration file. This means that it configures the server only in the directory the .htaccess file is in.

Can I have more than one htaccess file?

You can have more than one .htaccess file on your hosting account, but each directory or folder can only have one. For example, you can have separate .htaccess files in your root folder and another in a sub-folder. This allows you to set different server behavior based on directory structure.

Is htaccess mandatory for shared hosting?

No, .use of .htaccess is not mandatory. However, it is one of the only ways shared hosting users can get some control over their web hosting server. If you don’t plan on making server configuration changes on shared hosting, the file is unnecessary. What does .htaccess file contain?

What are error documents in htaccess?

One of your simple .htaccess basics is setting up error documents. Any .htaccess guide like this one will tell you that when a server receives a request, it responds by offering a document. Just like with HTML pages. Otherwise, it can pull that response from a particular application (as with Content Management Systems and other web apps).


1 Answers

$1 is the first captured group from your regular expression; that is, the contents between ( and ). If you had a second set of parentheses in your regex, $2 would contain the contents of those parens. Here is an example:

RewriteRule ([a-z0-9/-]+)-([a-z]+).html$ $1-$2.php [NC,L,QSA]

Say a user navigates to hello-there.html. They would be served hello-there.php. In your substitution string, $1 contains the contents of the first set of parens (hello), while $2 contains the contents of the second set (there). There will always be exactly as many "dollar" values available in your substitution string as there are sets of capturing parentheses in your regex.

If you have nested parens, say, (([a-z]+)-[a-z]+), $1 always refers to the outermost capture (in this case the whole regex), $2 is the first nested set, and so on.

like image 63
user428517 Avatar answered Oct 18 '22 00:10

user428517