我正在努力Git 列表(git 的 PHP 网络查看器)和 GIT Smart HTTP(S) 位于同一域 git.domain.ext 下。以下是实际设置:
- GIT Smart HTTP URL 的形式为 git.domain.ext/git/reponame.git
- GitList 通过 git.domain.ext/gitlist 提供服务
到目前为止一切顺利。一切正常。
我想要实现的目标如下:
- 任何形式为 git.domain.ext/*.git 的请求都应转到 git.domain.ext/git/*.git
- 任何其他请求都应默认为 git.domain.ext/gitlist
以下是我的 Apache 配置:
<VirtualHost *:443>
ServerName git.domain.ext
DocumentRoot /home/user/www/git.domain.ext
SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER
SetEnv GIT_PROJECT_ROOT /home/user/git/
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
<Location />
AuthName "Git Authentication"
AuthType Basic
Require valid-user
AuthUserFile /home/user/git/.htpasswd
</Location>
<LocationMatch "^/git/.*/git-receive-pack$">
AuthType Basic
AuthName "Git Access"
Require valid-user
AuthUserFile /home/user/git/.htpasswd
</LocationMatch>
[..]
</VirtualHost>
以下是 /home/user/www/git.domain.ext 下的 .htaccess
AuthName "Git Authentication"
AuthType Basic
Require valid-user
AuthUserFile /home/user/git/.htpasswd
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)\.git
RewriteRule ^(.*)\.git /git/$1.git [R=301,L]
RewriteRule ^(.*) /gitlist/ [R=301,L]
</IfModule>
使用原始 /git/*.git 的 Git 操作工作:
git clone https://[email protected]/git/test.git
Cloning into 'test'...
Password for 'https://[email protected]':
remote: Counting objects: 343, done.
remote: Compressing objects: 100% (322/322), done.
remote: Total 343 (delta 110), reused 149 (delta 12)
Receiving objects: 100% (343/343), 1.13 MiB | 1.36 MiB/s, done.
Resolving deltas: 100% (110/110), done.
不幸的是,对 /*.git 进行 git 操作不起作用:
git clone https://git.domain.ext/test.git
Cloning into 'test'...
Password for 'https://[email protected]':
fatal: https://[email protected]/test.git/info/refs?service=git-upload-pack not found: did you run git update-server-info on the server?
然而,重定向似乎有效:
curl --user user:pass https://git.domain.ext/test.git
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://git.domain.ext/git/test.git">here</a>.</p>
[..]
感谢您的帮助。
答案1
事实证明,总是mod_rewrite 错误 :-)
为了完成工作,我在.htaccess 中使用了以下内容:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)\.git
RewriteRule ^(.*)\.git /git/%{REQUEST_URI} [L]
RewriteRule (.*) /gitlist/ [R,L]
</IfModule>
问题出在之前的 中RewriteRule ^(.*)\.git /git/$1.git [R=301,L]
,它产生了 404 错误。奇怪的是,我只能使用 curl 的 --verbose 来发现它们。
答案2
据我所知,git 作为客户端不处理重定向。您是否希望以重定向而不是静默重写(即没有选项R=302
)的方式进行重写?