Вопрос

I am on ubuntu 12.04. Hosts file:

127.0.1.1 site.to.browse

Vhost:

<VirtualHost *:80>
    ServerName site.to.browse
    DocumentRoot "/path/to/website/root"
    <Directory /path/to/website/root>
        AllowOverride All
        Options All
    </Directory>
</VirtualHost>

On my android device that is connected to the same WIFI as the host I try to access this site with

192.168.178.57/site.to.browse

but I get a 404. When I browse just 192.168.178.57/ I get my default IT WORKS page.

Any ideas how to access the specified website?

Это было полезно?

Решение

You need to add

192.168.178.57 site.to.browse

to the hosts file on your android phone, so that it knows that http://site.to.browse/ refers to your server at 192.168.178.57.


If you want to access your vhost using http://192.168.178.57/site.to.browse instead, you'll need to do a local URL rewrite on the default vhost that redirects the page request to the desired vhost. So, in the Apache configuration for the default vhost, add

RewriteEngine On
RewriteRule ^/*site\.to\.browse/*(.*)$ http://site.to.browse/$1 [PT,L]

Unfortunately, this causes a discrepancy between the URL the client uses (say, http://192.168.178.57/site.to.browse/some/page.html) and the one that the server actually serves (http://site.to.browse/some/page.html). In particular, absolute paths (/some/page.html) will not be redirected correctly.

You can avoid this by reverse-proxying the internal vhost from the default vhost, using e.g. mod_proxy_html and/or mod_substitute to adjust URLs and paths in the content.

A much more practical approach, for testing, is to put everything interesting in your vhost inside a top folder, so that absolute URLs will only differ by the hostname part. For this, you need

RewriteEngine On
RewriteRule ^/*site\.to\.browse/*(.*)$ http://site.to.browse/site.to.browse/$1 [PT,L]

in the Apache configuration for the default vhost, and

RewriteEngine On
RewriteRule ^/*site\.to\.browse$ /site.to.browse/ [R,L]
RewriteCond %{REQUEST_URI} !^/*site\.to\.browse/
RewriteRule ^/*(.*)$ /site.to.browse/$1 [R,L]

in the vhost configuration; this latter part will redirect any http://site.to.browse/foo to http://site.to.browse/site.to.browse/foo, including http://site.to.browse/ to http://site.to.browse/site.to.browse/.

Obviously, you don't need to use site.to.browse as the folder name; you could just use e.g. mysite. The configuration would then be

RewriteEngine On
RewriteRule ^/*mysite/*(.*)$ http://site.to.browse/mysite/$1 [PT,L]

on the default vhost, and

RewriteEngine On
RewriteRule ^/*mysite$ /mysite/ [R,L]
RewriteCond %{REQUEST_URI} !^/*mysite/
RewriteRule ^/*(.*)$ /mysite/$1 [R,L]

in the site.to.browse vhost. The latter will redirect anything not under the mysite folder to under the mysite folder.

Другие советы

If you really want to access the site by http://192.168.178.57/site.to.browse/ then I would simply add the following line to your /etc/apache2/sites-available/default inside the default VirtualHost:

Alias /site.to.browse /path/to/website/root

<Directory /path/to/website/root>
    AllowOverride All
    Options All
</Directory>

Explanation: The VHost configuration you provided in your question will have no effect in this case.

The android client will send a request to the host identified by the IP Address, not the DNS-Name site.to.browse. Thus HTTP_HOST will be 192.168.178.57. So Apache won't take you vhost configuration since you don't request something from the host site.to.browse. Instead, apache's default config will be used which lives in /etc/apache2/sites-available/default.

As an alternative you could just replace the default It Works page by your own page, thus making the page available as http://192.168.178.57.

Of course you have to ensure that apache also binds to the machine's public IP. Since you get the It works page I take that as granted.

You need edit your vhost or .htaccess

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^site.to.browse$    index.php    [NC,L]  
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top