1

I have been stuck on this issue and have not been able to find a solution. I am using Apache 2.4.41.

I am attempting to create a RewriteRule on a site if the currently viewing folder has any files inside with a certain file extension (for this example, I will be using txt). I have Directory Indexing enabled so when a user clicks on a folder with one of these files inside, it'll rewrite using the rule I have set.

My issue is that I am not sure how I would detect this. I have looked into using a RewriteCond but I was unable to get it working and couldn't figure out what was going wrong.

RewriteCond %{REQUEST_FILENAME}/^(*.txt)$ -f

It does not error out or return a 500, but also does not perform what I am wanting at all.

I also tried using an If directive:

<If "-f %{REQUEST_URI} . '/*\.(txt)$'">
    # Do stuff
</If>

I am very certain I am using that entirely wrong, however I was unable to figure out if there was a correct way as I could not find any examples.

My goal is to have the directory and any subdirectory of it follow this setting I have set for it.

/thing1/test.txt <- YES
/thing1/subthing1/ <- YES
/thing2/txt.data <- NO
/thing2/subthing2/ <- NO

If the file in the directory doesn't exist, it shouldn't matter. Due to the way I have my apache configured, it would redirect to the 404 ErrorDocument.

The actual use and plan for this is to allow older virtual world software to connect to a world on my webserver through HTTP and explore, since a majority of older software do not support HTTPS. I run a small archival project, and it would make it a lot easier if I only had to force HTTP for it to work when necessary.

How can I achieve this?

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Ookinder
  • 11
  • 2
  • 1
    This isn't something you can really do in `.htaccess`, without the help of an external script (or possibly a _hack_). However, what are you actually trying to do? What problem are you trying to solve? What should happen when the directory contains a `.txt` file (you mention a "rewrite")? What should happen if it doesn't? Is this only for requests to the directory itself? Or what if the request is for `/directory/` or `/directory/`? – MrWhite Feb 07 '21 at 01:45
  • Agree. This seems like an odd requirement, which suggests an [xy problem](https://en.wikipedia.org/wiki/XY_problem). – Andrew Schulman Feb 08 '21 at 15:40
  • Edited to provide more details to what I am doing. – Ookinder Feb 09 '21 at 17:24
  • Sorry, I'm still not sure what you mean? What is "YES" and "NO" referring to? How does this relate to HTTP/HTTPS? – MrWhite Feb 09 '21 at 17:45
  • Forcing HTTP/HTTPS is exactly what I want to do with these folders. YES means it will do the something, NO means it won't. – Ookinder Feb 09 '21 at 19:08

0 Answers0