Asheeown Posted August 20, 2009 Share Posted August 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/ Share on other sites More sharing options...
Adam Posted August 20, 2009 Share Posted August 20, 2009 Given the nature in which it's being used, I think you'd be fine with something as simple as this: preg_match_all('#<NameVirtualHost (.*?)>#', $source, $vhost_matches); Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-902496 Share on other sites More sharing options...
Asheeown Posted August 20, 2009 Author Share Posted August 20, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-902500 Share on other sites More sharing options...
Asheeown Posted August 20, 2009 Author Share Posted August 20, 2009 I started working off what you gave me and I got it: <?php preg_match_all('/NameVirtualHost [^\*].*:.*\r/', $Contents, $vhost_matches); ?> Produces just what I need. Now I just have to figure out the virtualhosts Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-902506 Share on other sites More sharing options...
Adam Posted August 20, 2009 Share Posted August 20, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-902517 Share on other sites More sharing options...
Asheeown Posted August 20, 2009 Author Share Posted August 20, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-902519 Share on other sites More sharing options...
Adam Posted August 20, 2009 Share Posted August 20, 2009 Try this: <?php preg_match_all('/<VirtualHost (.*?)>(.*?)<\/VirtualHost>/s', $Contents, $vhost_matches); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-902542 Share on other sites More sharing options...
Asheeown Posted August 21, 2009 Author Share Posted August 21, 2009 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/ Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-903355 Share on other sites More sharing options...
Adam Posted August 21, 2009 Share Posted August 21, 2009 Have you checked the source? Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-903362 Share on other sites More sharing options...
Asheeown Posted August 21, 2009 Author Share Posted August 21, 2009 Have you checked the source? What's in the desired output is in the file. It's excluding everything wrapped in < > Quote Link to comment https://forums.phpfreaks.com/topic/171139-find-and-store-directives-in-httpconf/#findComment-903364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.