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 browseable gitweb interface. If you then start 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 the named file (i.e. in this example /etc/gitweb.conf
) as
a configuration for gitweb. You don't really need it in above
example; it is required only if your configuration file is in
different place than built-in (during compiling gitweb)
gitweb_config.perl or /etc/gitweb.conf
. See gitweb.conf(5) for
details, especially information about precedence rules.
If you use the rewrite rules from the example you might
also need
something 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 base URI 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
your Apache virtual host and gitweb configuration files in the
following way.
The virtual host configuration (in Apache configuration file)
should look 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 [QSA,L,PT]
# look for a public_git folder 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 following line 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
code referenced by $per_request_config
;
These configurations enable two things. First, each unix user
(<user>
) of the 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
second rewrite rule.
If you already use 'mod_userdir` in your virtual host or you
don't want to use the '~' as first character, just comment or
remove the second rewrite rule, and uncomment one of the
following according to what you want.
Second, repositories found in /pub/scm/
and /var/git/
will be
accessible through 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 like the 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 so that 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
the following. This configuration assumes that /var/www/gitweb
is
the DocumentRoot of your webserver, contains the gitweb.cgi
script and complementary static files (stylesheet, favicon,
JavaScript):
<VirtualHost *:80>
ServerAlias git.example.com
DocumentRoot /var/www/gitweb
<Directory /var/www/gitweb>
Options ExecCGI
AddHandler cgi-script cgi
DirectoryIndex gitweb.cgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.* /gitweb.cgi/$0 [L,PT]
</Directory>
</VirtualHost>
The rewrite rule guarantees that existing static files will be
properly served, whereas any other URL will be passed to gitweb
as PATH_INFO parameter.
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 and for fetching" section). A possible workaround for the
latter is the following: in your project root dir (e.g. /pub/git
)
have the projects named 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.com
DocumentRoot /var/www/gitweb
AliasMatch ^(/.*?)(\.git)(/.*)?$ /pub/git$1$3
<Directory /var/www/gitweb>
Options ExecCGI
AddHandler cgi-script cgi
DirectoryIndex gitweb.cgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.* /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 be cloned), 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 has a 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.