1

I've recently moved all my of my rules from a .htacess file into a .conf file. Now when I want to add a redirect rule to my website I have to edit the .conf file and sudo apachectl graceful.

I've now run into a problem in that I want to be able to dynamically generate my redirect rules from PHP. Back before when I had my .htacess file running it would have been relatively easy - just include a file there that PHP has control over and all redirects would work perfectly.

Now, I could still use an include file but i'm not sure (or comfortable with) PHP running apachectl graceful.

What is the best solution here? As I understand it with my current set up apache definately needs to be restarted in order for the .conf file to take affect, is that correct? If I returned to using .htaccess, could I just have one .htaccess in the root of the domain and restrict apache from using any other .htaccess files, so as not to slow down my server?

Chud37
  • 123
  • 7
  • I would be trying to design things such that I can have 1, or a couple of generic redirects. Then have a part of my PHP application that handling processing the URL and deciding how to handle it rather than dynamically creating loads of redirect rules. – USD Matt Jun 14 '18 at 12:47

1 Answers1

0

...but i'm not sure (or comfortable with) PHP running apachectl graceful.

Well, exactly. You shouldn't even be thinking about trying to edit your server config from PHP. This shouldn't even be possible, or if it is then you are opening a huge can of worms in terms of security.

...when I had my .htacess file running it would have been relatively easy - just include a file there that PHP has control over and all redirects would work perfectly.

Do you mean by allowing PHP to edit your .htaccess file? This also has potential security risks.

However, .htaccess or even your main server config, is not necessarily the best place for redirects anyway (assuming you're talking about external redirects). These redirects are usually because a resource/page has moved and no longer exists. In this case it is often more efficient to process the redirect in your server-side code (ie. PHP) once you have determined that a 404 would otherwise be returned. The redirect is a low-priority task. Serving content to your site visitors is a higher-priority. You don't need the redirect-check to be performed on every request.

MrWhite
  • 11,643
  • 4
  • 25
  • 40