Jump to content

Recursive Conditional Copy and Rename


John_A

Recommended Posts

I've got a dev server which, for reasons I won't go into, needs to have different .htaccess files for each site (and subfolders within them) within it. But not always.

What I need to do is: -

For the folder I put the PHP script in, and every folder under it (the whole tree under it): -

  • If there's a .htaccess file an no .htaccess.dev file, copy .htaccess and name the copy .htaccess.dev (and look in subfolders)
  • If there's a .htaccess file and also a .htaccess.dev file, do nothing (but keep looking for subfolders)
  • If there's neither a .htaccess or .htaccess.dev file, do nothing (but keep looking for subfolders)

I'd try to come up with something myself, but getting it wrong will be fairly catastrophic! I'm sure the conditional copy / rename bit is relatively straightforward, but it's the recursive searching of the whole file-system under the script's location that I'm not sure about...

Any help greatly appreciated, as always!

I have SSH access, if it's easier done that way maybe I'm approaching it wrong?

 

Link to comment
Share on other sites

A RecursiveDirectoryIterator (searches directories recursively) wrapped in a RecursiveCallbackFilterIterator (lets you give it a function to filter to just the files you want) wrapped in a RecursiveIteratorIterator (lets you foreach over it without having to care about the recursion) can find all the .htaccess files.

If you're considering a non-PHP approach because you only need to do this once then I would probably just do it manually. Surely there aren't too many of these, right?

Link to comment
Share on other sites

23 minutes ago, requinix said:

If you're considering a non-PHP approach because you only need to do this once then I would probably just do it manually. Surely there aren't too many of these, right?

Around 200! It is just a 1 off though.

Link to comment
Share on other sites

5 minutes ago, requinix said:

find . -name .htaccess | while read L; do [ -e "$L.dev" ] || cp "$L" "$L.dev"; done

Find all .htaccess files, and for each one test if there exists a .htaccess.dev and if not copy from the .htaccess.

Wow, if that works then that's excellent, thanks a lot!

I'll backup all just in case!

Link to comment
Share on other sites

Quote

for reasons I won't go into ... Around 200!

You probably should go into it. Experience says whatever you are doing, you are doing it the wrong way. You are asking how to solve your attempted solution which I would pretty much guarantee is not the right approach. If you tell us what the real problem is we can properly advise you.

Link to comment
Share on other sites

1 hour ago, benanamen said:

You probably should go into it. Experience says whatever you are doing, you are doing it the wrong way. You are asking how to solve your attempted solution which I would pretty much guarantee is not the right approach. If you tell us what the real problem is we can properly advise you.

OK, here's the background...

I used to use an environment variable to enable me to differentiate between my dev and production server, using this in .htaccess: -

<IfDefine DEV_SERVER>
  # dev server specific stuff here
</IfDefine> 

<IfDefine !DEV_SERVER>
  # production server specific stuff here
</IfDefine>

The environment variable "DEV_SERVER" was only ever set on my dev server, and never on production, obviously.

This approach worked a treat until my production server host installed Litespeed as a replacement for Apache. Litespeed doesn't like the above, and ignores everything.

I tried having different versions of .htaccess files for dev and production, but that's messy and dangerous, I like to be able to interchange all files at any time between the two.

My dev server runs Apache 2.4 and I have no plans to change this. I read that you can specify a different filename for .htaccess by putting this in httpd.conf: -

AccessFileName .htaccess.dev

But (I think) you can't set an order of preference here (like, say, DirectoryIndex), so I'm thinking the simplest solution so that I can have identical copies is have my dev server use .htaccess.dev instead of .htaccess - the production server will simply ignore any .htaccess.dev files.

If there's no difference (there doesn't always need to be, it depends on the site and htaccess requirements) then the .htaccess (if there is one) can just be copied and renamed. The dev server will use .htaccess.dev and the live server will still use .htaccess.

Problem is, I've manually done some already and so that's why if there is already a .htaccess.dev file then it should be left as is.

 

ETA: I realise this is now no longer a PHP problem, even though my first thought was to do it with a PHP script, it seems that was probably the more complicated approach.

Link to comment
Share on other sites

What are you doing in dev that is not going to be pushed to production? If you are serious about development you shouldn't be in a position where the web host is making changes to your server. A VPS is super super cheap these days and is the equivalent of a dedicated server as far as access and what runs on it. YOU have full control of the server. Of course that means you will need to have or get some server admin skills. There is also no reason you couldn't exactly mirror whatever production setup you have in dev between Vagrant, Containers such as Docker or Virtual Machines.

You should also be using version control like Git if you are not already. That will allow you to have dev branches and not affect the master codebase until you are ready to merge the changes.

Link to comment
Share on other sites

7 hours ago, benanamen said:

What are you doing in dev that is not going to be pushed to production? If you are serious about development you shouldn't be in a position where the web host is making changes to your server. A VPS is super super cheap these days and is the equivalent of a dedicated server as far as access and what runs on it. YOU have full control of the server. Of course that means you will need to have or get some server admin skills. There is also no reason you couldn't exactly mirror whatever production setup you have in dev between Vagrant, Containers such as Docker or Virtual Machines.

You should also be using version control like Git if you are not already. That will allow you to have dev branches and not affect the master codebase until you are ready to merge the changes.

Nothing, everything is the same there are just slight differences in the way things are done sometimes, e.g. parsing xml as php the syntax is slightly different in .htaccess due to PHP setups differing slightly. 99.9% of the time everything is identical, but there are sometimes a few lines in a .htaccess file somewhere that just have to differ.

I'm perfectly happy with the hosting I have, I pay for a managed service and it was actually myself who asked for Litespeed on the one in question, initially when it was touted as a faster, drop-in replacement for Apache but it's now evident that there are a few (rare cases) where things have to be done a little differently. I'm happy with the speed improvements that has brought, but at the same time don't want to mess with my dev server at this time as I have other sites live elsewhere that are not on Litespeed but use the same dev server.

Anyway, it is what it is and I am where I am. I'm quite happy with my setup (dev and multiple live servers), I don't do anything over-complicated. It worked perfectly before this issue (for 10+ years) and hopefully using requinix's solution it will continue to do so - it's no great hardship going forward if I occasionally need to make a .htaccess.dev file if there's a difference between dev and production. All I asked for was a way to quickly clone the existing .htaccess files as per the OP ;)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.