Home Ask Login Register

Developers Planet

Your answer is one click away!

Josiah February 2016

Using mod_rewrite to redirect .dev domains

In the Apache 2.4 docs on dynamic virtual hosts, it says:

Mass virtual hosts with mod_rewrite

Mass virtual hosting may also be accomplished using mod_rewrite, either using simple RewriteRule directives, or using more complicated techniques such as storing the vhost definitions externally and accessing them via RewriteMap. These techniques are discussed in the rewrite documentation.

I'm attempting to use mod_rewrite instead of mod_vhost_alias because I want it both ways: localhost/project and project.dev should point to the same folder, but either URL should work.

Here's my latest attempt (currently in an .htaccess), which gets me a lovely 500 error.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

If I do

RewriteRule ^(.*)$ http://localhost/%1/$1 [L,QSA]

I can access the files, but the URL changes (not what I want). I've tried a variety of permutations with and without slashes, RewriteBase, etc.

To be clear, I want project.dev/index.php and localhost/project/index.php to both be valid non-redirected references to /var/www/html/project/index.php. And I'd like to do this in a dynamic way, so I don't need to enter a new set of rules for every folder.

I'm not fixated on doing this with .htaccess - virtualhosts are ok too as long as they're dynamic and I can still access my sites using the localhost/ scheme and the other machines on the network can connect to the sample sites in the usual way (


anubhava February 2016

Try this rule:

RewriteEngine on

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

Post Status

Asked in February 2016
Viewed 3,880 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life