Search code examples
apache.htaccessmod-rewritemampmod-proxy

How can I change a localhost URL with mod_rewrite or mod_proxy?


I've got MAMP running (Apache:80 MySQL:3306), and my localhost has a number of folders containing projects:

127.0.0.1/
127.0.0.1/test1/
127.0.0.1/test2/

I've got my hosts file setup properly so jlc.dev/test1 and jlc.dev/test2 are working fine.

Where I'm having trouble is I'd like to have the URL test1.dev to run the app at /test1 and the URL test2.dev to run the app at /test2. (I'm not sure what the right term is there, but I hope I've got the point across.) Based on one of the responses in this post, it sounds like it can be achieved with mod_proxy or mod_rewrite in the .htaccess file.

What I'm assuming I do is direct test1.dev to 127.0.0.1 in my hosts file, and have an .htaccess file that looks at the referring domain and rewrites to /test1 appropriately.

Little help? Thank you!


Solution

  • You'll need to direct both test1.dev and test2.dev to 127.0.0.1. Then add this to the htaccess file in the document root:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^([^.]+)\.dev$ [NC]
    RewriteCond $1::%1 !^([^/]*)/.*::\1
    RewriteRule ^(.*)$ /%1/$1 [L]