SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    rewrite and virtual host prob

    Hi:
    greetings, hello, hola, sup?

    I want my rewrite rules to change www.domain.com to www.domain.local or at least for the hosts file to send requests to my devel server. Code first and "why?" afterwards.

    the vhost stuff:
    Code:
    #
    # www.domain.local (/etc/apache2/sites-available/www.domain.local)
    #
    <VirtualHost 192.168.1.151:80>
    	ServerAdmin mike.dcc@gmail.com
    	ServerName www.domain.local
    	ServerAlias domain.local
    	
    	# Indexes + Directory Root
    	DirectoryIndex index.php
    	DocumentRoot /var/www/www.domain.local
    	
    	# CGI Directory
    	ScriptAlias /cgi-bin/ /var/www/www.domain.local/cgi-bin/
    	<Location /cgi-bin>
    		Options +ExecCGI
    	</Location>
    	
    	# Logfiles
    	ErrorLog /var/www/www.domain.local/logs/error.log
    	CustomLog /var/www/www.domain.local/logs/access.log combined
    </VirtualHost>
    
    <VirtualHost 127.0.0.1:80>
    	ServerAdmin mike.dcc@gmail.com
    	ServerName www.domain.local
    	ServerAlias domain.local
    	
    	# Indexes + Directory Root
    	DirectoryIndex index.php
    	DocumentRoot /var/www/www.domain.local
    	
    	# CGI Directory
    	ScriptAlias /cgi-bin/ /var/www/www.domain.local/cgi-bin/
    	<Location /cgi-bin>
    		Options +ExecCGI
    	</Location>
    	
    	# Logfiles
    	ErrorLog /var/www/www.domain.local/logs/error.log
    	CustomLog /var/www/www.domain.local/logs/access.log combined
    </VirtualHost>
    The .htaccess rewrite stuff:
    Code:
    <IfModule mod_security.c>
    <Files async-upload.php>
    SecFilterEngine Off
    SecFilterScanPOST Off
    </Files>
    </IfModule>
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteBase /www.domain.local/
    RewriteCond %{HTTP_HOST} !^www.domain.local$ [NC]
    RewriteRule ^(.*)$ http://www.domain.local/$1 [R,L]
    
    RewriteBase /domain.local/
    RewriteCond %{HTTP_HOST} !^domain.local$ [NC]
    RewriteRule ^(.*)$ http://www.domain.local/$1 [R,L]
    
    # BEGIN WordPress
    RewriteBase /domain.local/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /domain.local/index.php [L]
    # END WordPress
    The hosts file:
    Code:
    127.0.0.1	localhost
    
    192.168.1.151 deimos deimos.local domain.local www.domain.local
    
    #this line below seemed to work but my CSS got trashed - no styling at all
    192.168.1.151 domain.com www.domain.com
    
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    Note: Perhaps after several attempts to make this work, I left a glaring error in one of the above files. If so, fixing it might not help.

    Simple reason why I want this:

    My wordpress site is hosted. wordpress stores URLs in a database. The hosted database contains the content. An export from hosted / import to local will still bring in .com URLs. I want to see my code changes happening locally with the real content from the hosted DB, not some lorem ipsum stuff that lacks the things I've added - some of which is used for ACL (access control). This is a must (i.e., using the external database while the local code executes). And until I click a link in the main nav (again, that comes from the WP DB) it works. The code is self aware. I figgers out whether it's executing on the host's server or mine and whether I want the local database or external.

    I'm tired of waiting on git pushes every time I make a change.

    I HAVE tried changing the local site tree from www.domain.local to www.domain.com (would need to change the hosts file every time I switch, but that isn't a bad compromise if there's no other solution) along with changes to the vhost code and rewrite rules but I'm not sure I've tried all the combos and permutations of that nightmare.

    Yes, I tell apache2 to reload the config when I change the vhost stuff. And firefox seems to handle the hosts file changes without a restart (surprise!)

    Thanks in advance for anyone who can help. Tell you what... if you like, when my partner and I gear up for first round funding, we'll send the winning solution provider(s) a free t-shirt 8) Yup. Folks who help out get at least a t-shirt.

    TIA again
    Mike

    P.S. HostMonster sets their server for local (mountain) time and you can forget MySQL timestamps from reflecting UTC. Beware!

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mike,

    Two quick things:

    1. Are "local requests" being made on the same computer or have you setup a test server to download to for testing?

    This question is to sort out the difference between 151... and 127... IMHO, they should not be mixed (but I stand the possibility of being overruled by Apache.org).

    2. <IfModule> wrappers are there to protect ID-10-T's from bringing down the server for unrecognized commands in the configuration file (httpd.conf, httpd-vhosts.conf and .htaccess).

    They don't hurt the first two because they're read only upon Apache start but if those are in an .htaccess file, they're very abusive of the server. Test once to be sure whether the module is enabled then remove the wrappers!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Mike,

    Two quick things:

    1. Are "local requests" being made on the same computer or have you setup a test server to download to for testing?

    This question is to sort out the difference between 151... and 127... IMHO, they should not be mixed (but I stand the possibility of being overruled by Apache.org).

    answer: I'm kinda unclear on your question. Say I'm viewing a page from a server on this box - the one i'm typing this reply on. It runs a test server. But I'm using the database on the web host's server - it contains the content. It works for the initial page request because I'm using the .local TLD (unofficial TLD, of course). Since wordpress' database supplies the top nav links, the page I view has links that point to the host's server. But I want the page served up from my test server. I mentioned all this in my post. Reason again.. so I don't have to do a git commit and push to my repository, switch screens to secure shell on the host's server, change branches, cd to the site's dir and pull in the changes... just to see the result.

    I think I threw out a red herring, though, sorry. Rewrite and virtual hosting has nothing to do with the prob because that handles incoming requests (even if the request is made from this machine, it's "incoming to the server". It's the hosts file that the browser looks at to determine what to do when I click a link. Just like it caches DNS resolves, it will look at the hosts file to see which IP to send the request to. That's how some malware/adware blockers work. You put the malicious site's URL in the hosts file under 127.0.0.1 or local host and if the page isn't on localhost (either you're not running a server, or it just isn't there) ... So, for example, an ad insertion pointed to by www.example.com/index.html would show a copy of my index.html on the page rather than the ad originally inteded to be there.

    I ditched all the vhost and rewrite crap to get back to normal and still didn't get what I wanted, but I need to revisit it. My external database connection occurs in PHP as my local page loads. works fine. The top nav links all point to my domain - the hosted site.

    Code:
    127.0.0.1 www.example.com
    # or
    localhost www.example.com # ?? not sure if first field can be a URL
    in my hosts file should "redirect" the request to me but now that I think of it, the reference to the external databse "MIGHT" also be redirected back to my test server. In that case - it might be my reason for using

    www.example.local

    for the local version. You see.. the gotcha is that wordpress settings ask for the site and home URL and sticks it in the database (which would be the external one unless I do an ugly hack) so changing to .local on my test site will just hose things up for the hosted site

    implied question: Maybe I can separate the two by specifying / differentiating between the external DB URL and the page URL by specifying the port number, but I'm not sure if the hosts file allows port specifications. hmmm...

    needs more thought and I have an appointment to get ready for.

    2. <IfModule> wrappers are there to protect ID-10-T's from bringing down the server for unrecognized commands in the configuration file (httpd.conf, httpd-vhosts.conf and .htaccess).

    They don't hurt the first two because they're read only upon Apache start but if those are in an .htaccess file, they're very abusive of the server. Test once to be sure whether the module is enabled then remove the wrappers!

    reply: Duly noted. Will fix. Where is the stinking end tag? </ifmodule>

    I think wordpress wrote that in when I turned on pretty permalinks, 'cause I sure as hell didn't write that. I cut out and moved WP's rewrite rule (I surrounded it with the # begin/end wordpress comment, but may have screwed up when I wrote the bit to allow file uploads. I'll fix that.

    I guess by "test once" you mean that if it seems to work, call it good.

    So... my appointment. Gottta run, but thank you for your time. Maybe this will be resolved soon.

    Mike -- crap! no preview link for quoted replies. hope and click, edit and preview

    Regards,

    DK
    Last edited by MikeCo; Jun 23, 2012 at 06:05. Reason: tidying things up

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mike,

    The first question related to whether you were looking at the domain via a test server or via the production server. IMHO, they really should be separate so you can thoroughly test the controls and changes they make to the database - you DON'T want to use the live database for that! That brings me to the advice that 127.0.0.1 should NOT be used on the production server as you should not be using it for (local) testing. If you choose to do so, it's better to use the production server with a protected directory nono or setup a subdomain for testing.

    In your hosts file, I've only ever used the IP address and the virtual domain so, while localhost and 127.0.0.1 are synonymous for humans, I doubt the same is true for computers. As for your question, the domain is ONLY the domain (no http:// protocol), i.e., www.example.com or example.com (or localhost) are legitimate for the second entry in the hosts file.

    External database? Too dangerous for me! I won't allow external links to my databases and, when I'm testing, I'll copy the database (or a section of it) to my test server so I don't adversely affect the database contents.

    Setting up WP on a (SEPARATE) test server is fine - use your domain name without the tld (which would be easy to backfill if you need to transfer/restore from the test server to the production server).

    Indeed, WP does add the <IfModule> wrapper but I've assumed that they got sick of the ID-ten-Ts whining about WP killing their websites (because they weren't set-up properly). I would, too, but I would have added a MASSIVE warning to newbies to verify the functionality then remove the wrapper! If it works once, why would it not work every time? Who is going to reconfigure the server to disable mod_rewrite? mod_security? mod_yadda_yadda?

    Preview links is the Advanced (which I use because there's so much more room to view original message and the reply).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK:

    I really see no danger messing with the external database. If I can have my way, I'll be able to add and view content to the live site (it's only for collaboration between me and my partner for now) and my partner can do the same. In the mean time, I can be changing the CSS, jQuery, and PHP and viewing the results on the test server with the devel code without my partner all of a sudden seeing some odd "access denied" message when he views a post. At the same time, I'd be able to go to the live site just by changing the tld and see that I DO get an access denied if I'm not logged in. I suppose all this is too much to understand or that I just don't know how to get the point across, so I guess I'm on my own.

    Thanks for the tips on how to be nice to the live server.

    Regards
    Mike

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Aw Mike, after my whining (giving tips), I gave you the answer you were looking for (subdomains/subdirectories) so please don't be cross with me!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm at a loss to see how that solves my prob. Using a subdomain on the hosted server leaves me with the one prob I want to avoid - having to push every change I make. I'd really prefer to not belabor my reasons for wanting things this way.

    DK: use your domain name without the tld (which would be easy to backfill if you need to transfer/restore from the test server to the production server).

    Me: my vhost code doesn't show it, but I've tried using example.local as as server alias. See no reason there'd be a difference and IIRC, my later post mentions my inadvertent red herring - the prob isn't with the rewrite rules in .htaccess or the virtual host directives. example.local gets me to the home page - i'll give a walkthrough of the process below. The only thing that seems to get close is to use the hosts file to redirect .com to localhost here on my test machine. Remember, the test server is on the same machine I use to browse the site. Sitting behind two routers and port 80 is closed to the outside world.

    DK: External database? Too dangerous for me! I won't allow external links to my databases and, when I'm testing, I'll copy the database (or a section of it) to my test server so I don't adversely affect the database contents.

    Me: Hostmonster DOES allow it. I'm not publicizing the URL, however. It's in a file of constants outside of ht_docs. WP usually puts it inside wp-config.php, but that looked risky so I did what I always do. They even mention it in articles on securing WP. If it makes you feel better, though, I can always clone the DB on the hosted server just to protect my precious, not ready for prime time site. Heh, Heh... There's only one or two pages that can be seen unless I or my partner logs in to WP AND the custom CMS we use to collaborate.

    Walkthrough:

    1. I browse to example.local and see my home page with the content on the hosted server's DB. The nav links supplied by the aforementioned external / hosted server DB are right except they end in .com

    2. Clicking a top nav link, now that I've added the redirect to the hosts file is giving me server errors, one of which suggests a recursion prob remedied by upping the limit with the LimitInternalRecursion directive. I put that in my vhosts file since it was more convenient from a readability standpoint. If I set the limit to 100, IIRC the error goes away but still no page.

    Clue: when I visit the hosted site, I can get to the admin page with example.com/wp-admin, but on the local site, I have to use example.local/cms/wp-admin. This was set up according to the WP codex article on "Giving Wordpress its Own Directory" AFAIK, everything is the same on both servers. The redirect is done in the index.php file with require('./cms/wp-blog-header.php') IIRC, and from the way I interpret WP's rewrite code , everything passes through that anyway - http://humanshell.net/2011/08/14/wor...nitialization/

    Anticipated response: If I can just browse to the link I click without getting a server error, I can deal with the recursion prob later.

    I'm upping the ante to 2 t-shirts. Final offer unless the person to solve this can prove a larger family - wife and children only.

    BTW: like the way you set up your tut on rewriting with the show/hide text. I think I'll read it.

    Cheers
    Mike

  8. #8
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As if I didn't already have my stuff in subdirectories which my posted code should reveal, but subdomains wouldn't help. The basic fact is the way wordpress handles links - GUIDs. Even if you export the database, which I clearly stated as a non-option, you have to modify the GUIDs as the article linked below states. I see the wordpress forum here has no answer, either.

    So google sucked on this endeavor and wordpress search wasn't much better. After exhausting all combinations and permutations of the keywords, I found this (below). Still don't know if it will work because I've yet to try and there is no such thing as an existing call to get_option() in any function.php I've seen, including the one in my theme. That includes the book _Wicked Wordpress Themes_ and the great article by Jefferey Way or Ian Stewart (sorry guys - I always get you two confused) on barebones wordpress theme devel. These guy actually tried what they preached before publishing. I know because what they say works.

    http://codex.wordpress.org/Running_a...y_of_WordPress

    Title: Running a Development Copy of WordPress
    not what i was searching for, but i might have used devel or development.

    "This is common for commercial sites, where you have a live production server but you also need a development and/or staging server, ..."
    WOW!!! PROFOUND!!! Why didn't I think of that? Wait! I damn sure did! Some poindexter told me it was lethal.

    Subheading: Run 2 Copies of WordPress from the same Database
    ANOTHER PROFOUND IDEA! Someone out there is creative, thinking, and pretty friggin' smart.

    Maybe it'll work. Makes life simple. One tab for the local site and optionally one for admin if I hide the admin bar. No extra noise, no accidental refresh the wrong page. Change some css or add some other functionality. Does it modify the db? Can I fix it if it does? Yup. Since I know what my theme options do to the wp database (set an option) I think I can undo anything. The damn thing was designed to be aple to reset ALL custom options and start from scratch - populating the custom options with the my default theme settings while remembering stored preset options unless I specificlly choose to clear those. Can I fix my custom db? Well, If i wrote the SQL to modify it and it got hosed up, I'd really suck if I couldnt ssh to the host and commandline mysql it back to health.

    Ok, now I would be able to modify the local code and test it with all the value added stuff already on the hosted db. And if I need to make a post to the live site, I can do it right from there. You should see how fubar two wp databases are when you export from one to another - especially when you change the page structure and heirarchy. Especially when a post ID on one is different from the other.

    Unlike some, I will be back to report whether this worked - with any extra info that may or may not be helpful to the community.

    Skiddabut doo didda de dum day...
    Mike


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •