使用 mod_rewrite 重定向 .dev 域
Using mod_rewrite to redirect .dev domains
在 Apache 2.4 docs on dynamic virtual hosts 中,它说:
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.
我正在尝试使用 mod_rewrite 而不是 mod_vhost_alias 因为我想要两种方式:localhost/project 和 project.dev 应该指向同一个文件夹,但是要么URL 应该可以。
这是我最近的尝试(目前在 .htaccess 中),它给我带来了一个可爱的 500 错误。
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/ [L,QSA]
如果我这样做
...
RewriteRule ^(.*)$ http://localhost/%1/ [L,QSA]
我可以访问这些文件,但是 URL 发生了变化(不是我想要的)。我已经尝试了各种有斜线和无斜线、RewriteBase 等的排列
明确地说,我希望 project.dev/index.php
和 localhost/project/index.php
都是对 /var/www/html/project/index.php
的有效非重定向引用。而且我想以动态方式执行此操作,因此我不需要为每个文件夹输入一组新规则。
我并不专注于使用 .htaccess - 虚拟主机也可以,只要它们是动态的,我仍然可以使用 localhost/ 方案访问我的网站,并且网络上的其他机器可以连接到示例站点以通常的方式 (192.168.1.22/project/index.php).
试试这个规则:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(.+)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/ [L]
在 Apache 2.4 docs on dynamic virtual hosts 中,它说:
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.
我正在尝试使用 mod_rewrite 而不是 mod_vhost_alias 因为我想要两种方式:localhost/project 和 project.dev 应该指向同一个文件夹,但是要么URL 应该可以。
这是我最近的尝试(目前在 .htaccess 中),它给我带来了一个可爱的 500 错误。
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/ [L,QSA]
如果我这样做
...
RewriteRule ^(.*)$ http://localhost/%1/ [L,QSA]
我可以访问这些文件,但是 URL 发生了变化(不是我想要的)。我已经尝试了各种有斜线和无斜线、RewriteBase 等的排列
明确地说,我希望 project.dev/index.php
和 localhost/project/index.php
都是对 /var/www/html/project/index.php
的有效非重定向引用。而且我想以动态方式执行此操作,因此我不需要为每个文件夹输入一组新规则。
我并不专注于使用 .htaccess - 虚拟主机也可以,只要它们是动态的,我仍然可以使用 localhost/ 方案访问我的网站,并且网络上的其他机器可以连接到示例站点以通常的方式 (192.168.1.22/project/index.php).
试试这个规则:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(.+)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/ [L]