Forum Moderators: phranque

Message Too Old, No Replies

Rewriting the domain part of a URL, locally hosted sandbox

         

KenS

11:29 am on Jan 6, 2012 (gmt 0)

10+ Year Member



Apologies if this is somewhat a basic question.

I have a web site that is written and hosted on a public server, lets say it is www.mysite.com. I have installed a local copy of Apache on my laptop, not connected to the outside world. Setting htdocs allows me to view the web site from my local file system, lets say from file:///c:/tempdir.

Links in the locally hosted web site work fine when they link to a relative path, but some links use the full URL and I want to configure .htaccess file so I can also view html pages with these absolute [mysite.com...] paths. I have tried numerous attempts at this using regular expressions in rewrite rules, but have not got it to work, and don't think it is the right way of re-writing the domain part of the URL.

In essense, I want URLs such as [mysite.com...] to be served from file:///c:/tempdir/afile.html.

I would be grateful for any assistance in achieving this.

Thanks in advance!

penders

12:39 pm on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rather than using .htaccess I think you need to setup a virtual host and redirect any requests to the real website to the localhost using your HOSTS file. This will also allow you to set up any number of websites on your test machine.

(As you have it at the moment, any requests to www.mysite.com don't go anywhere near your test site so trying to influence this using .htaccess will do nothing.)

KenS

3:22 pm on Jan 6, 2012 (gmt 0)

10+ Year Member



Thanks for your suggestion - it makes perfect sense, and allowed me to locate this example:
[johnbokma.com...]

However, following the example I still have problems.

I have updated the windows hosts file as below:

127.0.0.1 localhost # already existed
127.0.0.1 ks.mysite.com # Added.


I then added the following to httpd.conf

NameVirtualHost *:80
<VirtualHost *:80>
ServerName ks.mysite.com
DocumentRoot c:/tempdir # Same as the DocumentRoot setting
CustomLog logs/mysite.com.access.log combined
ErrorLog logs/mysite.com.error.log
</VirtualHost>

Now if I ping ks.mysite.com I get a reply from 127.0.0.1, so the hosts file is doing its thing, but if I browse to [ks.mysite.com...] I get "virtual host not known on this server".

Am I doing something obviously wrong? I have tried starting and stopping Apache after making the changes.

Ideally i would like it to be www., rather than ks., as the subdomain, but one step at a time.

Thanks.

KenS

3:26 pm on Jan 6, 2012 (gmt 0)

10+ Year Member



[oops, only when posting this did I realize that ks dot mysite dot com was a genuine address. The address I really used was not a genuine subdomain.]

penders

4:21 pm on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, I have a similar setup. I can't see anything obviously wrong with what you have. Is this the correct httpd.conf file?!

I actually add my virtual host directives to httpd-vhosts.conf and link to this in httpd.conf like so:
# Virtual hosts 
Include conf/extra/httpd-vhosts.conf

...keeps things separate, but adding directly to httpd.conf should be OK.

And then would add the virtual host directives along the lines of...

# example.com
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName www.example.com
DocumentRoot D:/WWW/vhosts/example.com/public_html
ErrorLog D:/WWW/vhosts/example.com/logs/error.log
TransferLog D:/WWW/vhosts/example.com/logs/access.log
</VirtualHost>


And update my HOSTS file, like you have done:
127.0.0.1 www.example.com


I do tend to use absolute paths, but I think relative paths should be OK. As you can probably see from my folder structure I have a folder for each domain in my vhosts folder and inside this folder I have public_html or htdocs (to match the live server).

I often do it slightly different and use "local.example.com" in my virtual host. So my local test server is accessible via the "local" subdomain and the live site is still accessible via "www." - but I've not usually had to deal with hardcoded absolute URLs.

penders

4:32 pm on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is a comment in my httpd-vhosts.conf file:
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.


And then the generic localhost virtual host follows this...
# localhost
<VirtualHost *:80>
ServerAdmin admin@localhost
DocumentRoot c:/xampp/htdocs
ServerName localhost
ErrorLog logs/error.log
</VirtualHost>


My virtual hosts follow this.

----

Also, since my virtual hosts are in a different folder structure, I also need to include:
<Directory D:/WWW/vhosts>
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>