1

Ok, this is in conjunction with my other post: Rewrite not working in .htaccess file

However, this is not a repost of the same question. This time I am only focusing on the first part of my .htaccess file.

    SetEnv HTTPS on

<IfModule mod_rewrite.c>
    RewriteEngine On
Options -Indexes
    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA]
</IfModule>

I tested it here: https://htaccess.madewithlove.be/ and got the following from the site - debugging info:

My Questions:

  1. Is this site fully functional (save what they call out in at the bottom of the page) ?

  2. What is causing my last line to not get met?

I reached out to the developer who setup this .htaccess file, and unfortunately, he was not able to answer why the last line was not met. He was honest and said that this part of the Linux world he was not the most versed in, so he ended up not being a good resource -- hence my very first question. I am a newbie to rewrite rules, so I do apologize if this is something simple and I am just missing it.

Please let me know if this post needs more clarification or to be formatted better. Thank you to everyone who is able to assist or explain anything here! :-)

MrWhite
  • 11,643
  • 4
  • 25
  • 40

1 Answers1

2

Your code looks "OK" and the output you are seeing is expected.

RewriteRule (.+) index.php?p=$1 [QSA]

In your example, you are requesting the document root and yes, this line will not match the document root in a .htaccess context because the URL-path is empty. The regex .+ matches 1 or more characters.

But this is probably OK and intentional, index.php should still receive the request (because of mod_dir and the DirectoryIndex), but without the p URL parameter. I would expect your PHP code to check for the presence of the URL parameter and handle this accordingly.

Instead, test a request for example.com/foo, that contains a URL-path. This will be caught by your RewriteRule directive.

If you wanted to always have a p URL parameter (and perhaps reject the request otherwise) then you could change the RewriteRule pattern to (.*) (0 or more characters). This will then match the document root and you would end up with an empty p URL parameter. However, there is really no advantage in doing this.


Aside: The madewithlove htaccess tester gets referenced a lot, however, I would treat the results with caution as I've found it to be quite buggy. (I have submitted a number of bug reports myself in the past.) For example, the following example results in the wrong backreference being reported: https://htaccess.madewithlove.be?share=352db4bc-22a0-5748-9d68-93a7f498f542 (the resulting URL should be /foo, but it reports /foobar, which is the URL from the request.)

MrWhite
  • 11,643
  • 4
  • 25
  • 40