Apache httpd.conf && .htaccess directives written in PCRE?

Apache httpd.conf && .htaccess directives written in PCRE? - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, apache, httpd.conf, regular-expression, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I look at the directives syntax of Apache configuration files like httpd.conf && .htaccess and I must say it looks quite similar to PCRE syntax (that stand for Perl Compatible Regular Expressions).



But, is the directives syntax really a Regular-expressions syntax ?



I am not quite familiar with regular expressions to strictly determine and thus aim the question to webmasters and/or programmers who have mastered or became experienced with it enough to answer.


Solution :


it looks quite similar to PCRE syntax




Yes, many of the parameters to Apache directives do take regular expressions (regex) and the "flavour" of regex used is indeed PCRE.



This applies to mod_rewrite and in fact any other Apache module that uses regex.



There are some minor differences (and additions) in the syntax used by Apache, but this is primarily because there are no regex "delimiters" (commonly /) to mark the start and end of the regex, so slashes do not need to be (backslash) escaped.



Also, because spaces are used as delimiters in Apache directives, literal spaces must be backslash escaped in the regex itself. Or, the regex can be surrounded in double quotes.



And since there are no regex delimiters, there are no pattern modifiers, such as i (eg. /regex/i) for case-insensitive matches. Instead, there are flags in mod_rewrite, eg. NC (NOCASE) for case-insensitive matches.



In addition to the normal syntax, you can prefix the pattern with ! to negate the regex. This makes it trivial to make a truthy expression when something does not match. mod_rewrite also has an additional backreference for matching groups in the RewriteCond CondPattern ie. %1 as well $1.



With Apache 2.4 you now have Apache Expressions, this does put back delimiters and pattern modifiers.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect