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:


#
# 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:


<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:


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!

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).

  1. <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

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

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

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

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/wordpress-initialization/

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

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_Development_Copy_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