1

We inherited some code recently and I'm reviewing the .htaccess file of one of the sites. I understand what the end result of most of the rewrite rule sets are, I'm just unsure of some of the syntax. Background info: CentOS 7 with Apache 2.4, running the Concrete5 CMS.

Example 1:

RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I understand that %{HTTPS} is one of the special variables provided by mod_rewrite that evaluates to either on or off. In a previous rule, I see RewriteCond %{HTTPS} =on which makes more sense and goes along with what the documentation shows here. So in the above example, I have the following questions:

  1. What is the trailing "s" for in %{HTTPS}s
  2. Why does the pattern use ^on instead of =on. Is this valid?
  3. I understand that anything in parenthesis can be back referenced with %1, but it's placement here doesn't really make sense to me. If %{HTTPS} is going to evaluate to on or off, then placing (s)| in Pattern is going to make it never match the condition, right? And what's the PIPE for?

Example 2:

#redirect calls to index.php without get parameters to non-www
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{THE_REQUEST} /index\.php\s [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

I'm not quite understanding the second RewriteCond.

  1. What is the \s doing at the end of index.php?

Example 3:

Lastly, we have

#Standard C5 pretty urls
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}/index.html !-f 
RewriteCond %{REQUEST_FILENAME}/index.php !-f 
RewriteRule . index.php [L]

In the RewriteRule, the pattern is . which in regex means "any character".

  1. Is this rule saying "If the above conditions are met, replace any character with index.php"? What is this rule doing?
-1

Please ignore this answer, it is wrong in many ways.

Example 1:

This rule is for forwarding non-HTTP connections to HTTPS connections. So:

  1. The trailing s is for SSL.
  2. ^on is used to check "if HTTP is NOT active". ^ means negative match.
  3. The references with % sign refer to RewriteCond regular expressions. In this case, %1 refers to the (s) capture group, and therefore %1 is s when HTTP is not active.

The way this RewriteRule has been written is a bit over-complicated, I think there could have been a simpler way to implement it. However, it does what it is designed to do.

Example 2:

  1. The \s matches white space. So, this RewriteRule matches /index.php, that is, a space after .php. I think that the second RewriteCond makes this rule not work at all, unless some part of the website software generates incorrect URLs.

Example 3:

  1. It means, "If the above conditions are met, replace any string in the URL with index.php". The . simply matches any string, and index.php is the replacement used. The match criteria in this RewriteRule does not need to be any more specific, since any non-existing URL is sent to index.php.
  • 1
    #1 The trailing "s" is so the "s" can be captured in the CondPattern. #2 ^ is the start of string anchor (not a "negative match"). #3 The pipe would seem to be redundant, since the TestString could never be empty (because of the trailing "s"). #4 The \s is required in order to only match index.php at the end of the URL-path. (This is checking against THE_REQUEST, not REQUEST_URI) It prevents URLs with additional path information or query strings from being rewritten (the former would then be lost in the rewrite). – MrWhite Jan 23 '17 at 11:49
  • 1
    #5 The . matches any URL, except for the document root (when used in a .htaccess context). Note that the RewriteRule is processed first (before the RewriteCond directives that precede it). So, this ensures that all URLs, except for the document root are processed (probably avoiding a rewrite loop since mod_dir will automatically request the DirectoryIndex in this case). – MrWhite Jan 23 '17 at 11:50
  • 1
    This answer gets just about everything wrong! – Jasper Oct 15 '17 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.