htaccess用于虚拟主机的URL重定向

时间:2018-10-12 00:32:22

标签: apache .htaccess

我有一个运行3个虚拟主机的Apache服务器:

<VirtualHost *:80>
    DocumentRoot "/var/www/sub1"
    ServerName sub1.domain.com

    # Other directives here
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/sub2"
    ServerName sub2.domain.com

    # Other directives here
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/sub3"
    ServerName sub3.domain.com

    # Other directives here
</VirtualHost>

我想在sub3上设置URL重定向,以便在请求sub3.domain.com/go/7dj29时将其重定向到sub3.domain.com/go/redirect.php?id=7dj29

我在.htaccess中创建了一个/var/www/sub3

RewriteEngine On
RewriteRule ^\/go\/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1 [QSA,L]

但是,我收到的是404,而不是重定向;我在做什么错了?

Not Found
The requested URL /go/7uja8 was not found on this server.

1 个答案:

答案 0 :(得分:1)

以下规则有效。

RewriteRule ^go/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1

要使apache从.htaccess加载规则,您至少需要 AllowOverride FileInfo,但是您可以直接在VirtualHost上下文中对其进行设置。