All of those examples use request rewriting, and need mod_rewrite
(or equivalent; examples below are written for Apache).
Single URL for gitweb and for fetching
If you want to have one URL for both gitweb and your http://
repositories, you can configure Apache like this:
<VirtualHost *:80> ServerName git.example.org DocumentRoot /pub/git SetEnv GITWEB_CONFIG /etc/gitweb.conf # turning on mod rewrite RewriteEngine on # make the front page an internal rewrite to the gitweb script RewriteRule ^/$ /cgi-bin/gitweb.cgi # make access for "dumb clients" work RewriteRule ^/(.*\.git/(?!/?(HEAD|info|objects|refs)).*)?$ \/cgi-bin/gitweb.cgi%{REQUEST_URI} [L,PT]</VirtualHost>
The above configuration expects your public repositories to live under/pub/git
and will serve them as http://git.domain.org/dir-under-pub-git
,both as clonable Git URL and as browsable gitweb interface. If you thenstart your git-daemon[1] with --base-path=/pub/git --export-all
then you can even use the git://
URL with exactly the same path.
Setting the environment variable GITWEB_CONFIG
will tell gitweb to use thenamed file (i.e. in this example /etc/gitweb.conf
) as a configuration forgitweb. You don’t really need it in above example; it is required only ifyour configuration file is in different place than built-in (duringcompiling gitweb) gitweb_config.perl or /etc/gitweb.conf
. Seegitweb.conf[5] for details, especially information about precedencerules.
If you use the rewrite rules from the example you might also needsomething like the following in your gitweb configuration file(/etc/gitweb.conf
following example):
@stylesheets = ("/some/absolute/path/gitweb.css");$my_uri = "/";$home_link = "/";$per_request_config = 1;
Nowadays though gitweb should create HTML base tag when needed (to set baseURI for relative links), so it should work automatically.
Webserver configuration with multiple projects' root
If you want to use gitweb with several project roots you can edit yourApache virtual host and gitweb configuration files in the following way.
The virtual host configuration (in Apache configuration file) should looklike this:
<VirtualHost *:80> ServerName git.example.org DocumentRoot /pub/git SetEnv GITWEB_CONFIG /etc/gitweb.conf # turning on mod rewrite RewriteEngine on # make the front page an internal rewrite to the gitweb script RewriteRule ^/$ /cgi-bin/gitweb.cgi [QSA,L,PT] # look for a public_git directory in unix users' home # http://git.example.org/~<user>/ RewriteRule ^/\~([^\/]+)(/|/gitweb.cgi)?$/cgi-bin/gitweb.cgi \[QSA,E=GITWEB_PROJECTROOT:/home/$1/public_git/,L,PT] # http://git.example.org/+<user>/ #RewriteRule ^/\+([^\/]+)(/|/gitweb.cgi)?$/cgi-bin/gitweb.cgi \ [QSA,E=GITWEB_PROJECTROOT:/home/$1/public_git/,L,PT] # http://git.example.org/user/<user>/ #RewriteRule ^/user/([^\/]+)/(gitweb.cgi)?$/cgi-bin/gitweb.cgi \ [QSA,E=GITWEB_PROJECTROOT:/home/$1/public_git/,L,PT] # defined list of project roots RewriteRule ^/scm(/|/gitweb.cgi)?$ /cgi-bin/gitweb.cgi \[QSA,E=GITWEB_PROJECTROOT:/pub/scm/,L,PT] RewriteRule ^/var(/|/gitweb.cgi)?$ /cgi-bin/gitweb.cgi \[QSA,E=GITWEB_PROJECTROOT:/var/git/,L,PT] # make access for "dumb clients" work RewriteRule ^/(.*\.git/(?!/?(HEAD|info|objects|refs)).*)?$ \/cgi-bin/gitweb.cgi%{REQUEST_URI} [L,PT]</VirtualHost>
Here actual project root is passed to gitweb via GITWEB_PROJECT_ROOT
environment variable from a web server, so you need to put the followingline in gitweb configuration file (/etc/gitweb.conf
in above example):
$projectroot = $ENV{'GITWEB_PROJECTROOT'} || "/pub/git";
Note that this requires to be set for each request, so either$per_request_config
must be false, or the above must be put in codereferenced by $per_request_config
;
These configurations enable two things. First, each unix user (<user>
) ofthe server will be able to browse through gitweb Git repositories found in~/public_git/
with the following url:
http://git.example.org/~<user>/
If you do not want this feature on your server just remove the secondrewrite rule.
If you already use mod_userdir
in your virtual host or you don’t want touse the '~' as first character, just comment or remove the second rewriterule, and uncomment one of the following according to what you want.
Second, repositories found in /pub/scm/
and /var/git/
will be accessiblethrough http://git.example.org/scm/
and http://git.example.org/var/
.You can add as many project roots as you want by adding rewrite rules likethe third and the fourth.
PATH_INFO usage
If you enable PATH_INFO usage in gitweb by putting
$feature{'pathinfo'}{'default'} = [1];
in your gitweb configuration file, it is possible to set up your server sothat it consumes and produces URLs in the form
http://git.example.com/project.git/shortlog/sometag
i.e. without gitweb.cgi part, by using a configuration such as thefollowing. This configuration assumes that /var/www/gitweb
is theDocumentRoot of your webserver, contains the gitweb.cgi script andcomplementary static files (stylesheet, favicon, JavaScript):
<VirtualHost *:80>ServerAlias git.example.comDocumentRoot /var/www/gitweb<Directory /var/www/gitweb>Options ExecCGIAddHandler cgi-script cgiDirectoryIndex gitweb.cgiRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^.* /gitweb.cgi/$0 [L,PT]</Directory></VirtualHost>
The rewrite rule guarantees that existing static files will be properlyserved, whereas any other URL will be passed to gitweb as PATH_INFOparameter.
Notice that in this case you don’t need special settings for@stylesheets
, $my_uri
and $home_link
, but you lose "dumb client"access to your project .git dirs (described in "Single URL for gitweb andfor fetching" section). A possible workaround for the latter is thefollowing: in your project root dir (e.g. /pub/git
) have the projectsnamed without a .git extension (e.g. /pub/git/project
instead of/pub/git/project.git
) and configure Apache as follows:
<VirtualHost *:80>ServerAlias git.example.comDocumentRoot /var/www/gitwebAliasMatch ^(/.*?)(\.git)(/.*)?$ /pub/git$1$3<Directory /var/www/gitweb>Options ExecCGIAddHandler cgi-script cgiDirectoryIndex gitweb.cgiRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^.* /gitweb.cgi/$0 [L,PT]</Directory></VirtualHost>
The additional AliasMatch makes it so that
http://git.example.com/project.git
will give raw access to the project’s Git dir (so that the project can becloned), while
http://git.example.com/project
will provide human-friendly gitweb access.
This solution is not 100% bulletproof, in the sense that if some project hasa named ref (branch, tag) starting with git/
, then paths such as
http://git.example.com/project/command/abranch..git/abranch
will fail with a 404 error.