Jump to content

Find and store directives in http.conf


Asheeown

Recommended Posts

So I am making a linux migration script and my third step is to transfer all the VirtualHost directives and the NameVirtualHost to the new machine.

 

For the NameVirtualHost assignment I need to get any and all lines that look like this:

NameVirtualHost 192.168.1.170:80

And save them in an array so I can add them to the new server, there are usually only one of those entries but multiples can occur

 

For the VirtualHost directives I'll give an example below

<VirtualHost 192.168.1.170:80>

# Created on Aug 20, 2009

ServerAdmin webmaster@test.com

DocumentRoot /home/Test/htdocs

ServerName www.test.com

ServerAlias test.com

AddHandler server-parsed htm html shtml

AccessFileName .htaccess

CustomLog /home/Test/Logs/access_log combined

<Directory />

AllowOverride All

</Directory>

<Directory /home/Test/htdocs>

Options Indexes

Options +Includes

</Directory>

ScriptAlias /cgi-bin/ /home/Test/cgi-bin/

</VirtualHost>

 

I figured since the VirtualHost directive is always the same, but the IP may be different it shouldn't be too hard.  However, I never got into regex and I can't figure out how to get these things and store them in variable for later use.

Link to comment
Share on other sites

preg_match_all('#<NameVirtualHost (.*?)>#', $Contents, $vhost_matches);

 

Produces two empty arrays within $vhost_matches

 

If I take away the # symbols in the pattern I get three values $vhost_matches[0] $vhost_matches[1] $vhost_matches[2] but all they have in them is "NameVirtualHost"

Link to comment
Share on other sites

Just so you know it doesn't match which regex delimiter you use. People have their own preference, I just seem to think I have to escape less using hash.

 

And.. well if it works, just wrap the part you want to match in brackets:

 

preg_match_all('/NameVirtualHost ([^\*].*:.*)\r/', $Contents, $vhost_matches);

Link to comment
Share on other sites

That works great.  Now for my VirtualHosts one, I made one, and like I said, I'm horrible at this.

 

<?php
preg_match_all('/\<VirtualHost [^\*].*?\>.*\<\/VirtualHost\>/s', $Contents, $vhost_matches);
?>

 

Only problem is it takes away every directive, starting with <VirtualHost> they just don't show up in the output.  There are other directives inside <VirtualHost> such as:

<Directory>

<Location>

And things of that nature, any idea of why that is happening?

 

Link to comment
Share on other sites

Still excludes all directives inside.

 

Output:

# Created on Aug 20, 2009

ServerAdmin webmaster@test.com

DocumentRoot /home/Test/htdocs

ServerName www.test.com

ServerAlias test.com

AddHandler server-parsed htm html shtml

AccessFileName .htaccess

CustomLog /home/Test/Logs/access_log combined

 

AllowOverride All

 

 

Options Indexes

Options +Includes

 

ScriptAlias /cgi-bin/ /home/Test/cgi-bin/

 

Desired Output:

# Created on Aug 20, 2009

ServerAdmin webmaster@test.com

DocumentRoot /home/Test/htdocs

ServerName www.test.com

ServerAlias test.com

AddHandler server-parsed htm html shtml

AccessFileName .htaccess

CustomLog /home/Test/Logs/access_log combined

<Directory />

AllowOverride All

</Directory>

<Directory /home/Test/htdocs>

Options Indexes

Options +Includes

</Directory>

ScriptAlias /cgi-bin/ /home/Test/cgi-bin/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.