简体   繁体   中英

how to redirect “example.com” to “localhost/example.com”

I am using MAMP to develop a Wordpress site locally. I've managed to map the domain "example.com" to localhost (127.0.0.1) in my hosts file like so:

127.0.0.1    example.com

Consequently, "example.com" redirects to my document root:

example.com --> /Applications/MAMP/htdocs

This is fine if I'm working on a single site; all my projects files go in 'htdocs'. The problem is I am working on multiple sites simultaneously, therefore I need more specific redirects, like so:

example1.com --> /Applications/MAMP/htdocs/example1.com
example2.com --> /Applications/MAMP/htdocs/example2.com
example3.com --> /Applications/MAMP/htdocs/example3.com

Is this possible?

It's possible with a minor change:
You can create directories under your root path for every domain:

/Applications/MAMP/htdocs/example1
/Applications/MAMP/htdocs/example2
/Applications/MAMP/htdocs/example3

Then you can add a rule in your .htaccess file for 301 redirect:

RewriteCond %{HTTP_HOST} ^(www.)?example1.com$
RewriteRule ^(/)?$ example1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?example2.com$
RewriteRule ^(/)?$ example2 [L]
RewriteCond %{HTTP_HOST} ^(www.)?example3.com$
RewriteRule ^(/)?$ example3 [L]

If all 3 domains point to the same server - it'll work (I've done it with two domains in the past).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM