Since on an iPad we cannot edit the hosts file (without jailbreaking), how can we arbitrarily redirect web traffic to another url?
This would be important for someth
It's also possible to use Weblock - AdBlock for iOS app (available for $1.99 here: https://itunes.apple.com/us/app/weblock/id558818638?mt=8) to create web traffic redirects.
This allows you to redirect any traffic matching certain rule to specified IP address. This will emulate adding an entry to /etc/hosts on your iOS device. If the hostname set in requests is handled by the IP you direct your traffic to, you can use this to test private API or even sniff traffic sent from other apps or websites. This unfortunately works only for http/https connections.
All this can be done only while on Wi-Fi (one of Weblock's limitations). There main advantage is that you can easily configure everything from your iOS device and there is no need to mess with DNS/proxy server configuration.
Here's an example:
macbook-pro-tk:~ kpr$ nc -l -v -v 1234 GET http://www.google.com/ HTTP/1.1 Host: www.google.com Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Proxy-Connection: keep-alive PREF=ID=7722bc3c844a7c26:TM=1402073839:LM=1402073839:S=5bSJJsM2p0HgUP7L User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D201 Safari/9537.53 Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: keep-alive
Weblock is also good to selectively redirect some URL's with regular expressions. You could redirect queries to certain endpoint only, while all other queries go to the IP returned from the DNS. This actually allows for even more fitting configuration that /etc/hosts does.
Example: If I create a URL redirect rule for htt*://somedomain.com/api/login* and some IP and port, I will see only traffic from this URL at this IP and port, while all other traffic to somedomain.com will go directly to the IP returned by the DNS. Notice it will work for both /api/login and /api/login?someparam=somevalue thanks to the wildcard * sign at the end of the rule.
You can also use http://xip.io/ using the instructions on that page you can enter the ip address and it will redirect you to the relevant local ip.
I found you just have to modify the Wifi settings in your iPad to use the IP address of your development machine as an HTTP proxy (as explained in the aforementioned article):
That way, it's enough to be able to access your web application on your iPad by entering the url of the virtual host (e.g. local.mywebapp.com
). It's easy and quick, but unlike Will Koehler's solution, you will however not be able to access Internet from the iPad. But most of the time it's not really a problem, since you just want to test your own application.
If you already have an Apache server where you're doing dev, you can easily use it as a forward proxy. This is particularly useful for WordPress sites, which really love to use the full absolute URL.
Ubuntu example below:
The first step is to edit the /etc/hosts
file in your dev server. Add the server's local IP, pointing to your site.
127.0.0.1 dev.mysite.com
This hosts file will be used by your Apache proxy when it tries to resolve requests from your iPhone / iPad. So let's setup the Apache part now...
You may need to install some modules first.
sudo apt-get install libapache2-mod-proxy-html
sudo a2enmod proxy proxy_http proxy_html
sudo apache2ctl graceful
Then create a virtual host file, for example /etc/apache2/sites-available/my-proxy
Listen *:8080
<VirtualHost *:8080>
ProxyRequests On
<Proxy *>
Order Deny,Allow
Deny from all
Allow from 192.168.1.0/24
</Proxy>
</VirtualHost>
Enable the vhost, and restart Apache:
sudo a2ensite my-proxy
sudo apache2ctl graceful
Then go to Settings > Wi-Fi > Your Network and configure a "Manual" proxy. Enter the IP of your Apache server, and the port. That's it!
The <Proxy *>
block ensures that only people on my local network can use this proxy. Strictly limiting access is essential if you are using a forward proxy. The ip2cidr page will be helpful at this point. (As an extra measure, the :8080 port is blocked by my firewall.)
You could setup an internal DNS server on your network (if one does not exist already) and setup an A record. Then ensure your DHCP is set to return said DNS server