大网数据广告活动
网站运营与故障问答

Using Apache's rewrite module to set up local host names for development

· 52CE 我爱测

I have several websites on my development machine under Apache's htdocs folder. Apache Directory root is htdocs, but I want to be able to access each different site there by individual domain names. For example, I have htdocs/dev…

问题原文

原帖链接 · Nick Rolando · CC BY-SA 3.0

I have several websites on my development machine under Apache's htdocs folder. Apache Directory root is htdocs, but I want to be able to access each different site there by individual domain names. For example, I have

htdocs/dev_site_com
htdocs/cre_site_com

I have added these lines to my hosts file

127.0.0.1       dev.site.localhost

127.0.0.1       cre.site.localhost

Now I want to add an .htaccess file in the htdocs to redirect dev.site.localhost request to htdocs/dev_site_com or 127.0.0.1/dev_site_com

I want to do this so that all of my relative URLs in my site work locally. If I link to a stylesheet in the dev_site_com site using the path /css/style.css, it will work in production, but not in development, because the root is htdocs and not htdocs/dev_site_com

I've never been good with mod_rewrite.. but this is what I have so far..

<IfModule mod_rewrite.c>

    Options +FollowSymLinks
    RewriteEngine on

    RewriteCond %{REQUEST_URI} ^dev\.site\.localhost
    RewriteRule %1 [L]

</IfModule>

and nothing is happening

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

You would not usually use rewrite rules to set up different host names on a local server. Instead you would edit your Apache configuration and set up new virtual hosts.

<VirtualHost *:80>
    DocumentRoot /var/www/htdocs/dev_site_com
    ServerName dev.site.localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/htdocs/cre_site_com
    ServerName cre.site.localhost
</VirtualHost>

You don't say what type of operating system you are running. If I were doing this on my Ubuntu Linux server I would create two files:

  • /etc/apache2/sites-available/dev
  • /etc/apache2/sites-available/cre

These contain the the virtual host directive for each site respectively.

I would then enable each site with the command a2ensite and restart Apache:
sudo a2ensite dev && sudo a2ensite cre && sudo service apache2 restart.

Here is a full guide to virtual host setup on Ubuntu.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2013-10-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。