Jump to content
Returning Members: Password Reset Required ×

Recommended Posts

Posted

This was just something I had to setup again recently and I thought I'd post it this time around. This snippet allows for any website utilizing .htaccess to be a go-get repository by redirecting traffic from go get commands (contains go-get=1 in the request) to GitLab or another git repository hosting website. So for example, rather than importing my projects from "gitlab.com/spirited/projectname", I'd use my domain "spirited.io/projectname". That allows me to either redirect human traffic to the project page on my website or redirect go import traffic to the actual repo.

Link to most up-to-date: https://gitlab.com/-/snippets/2121085

.htaccess

# BEGIN GolangRedirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (^|&)go-get=1($|&)
RewriteRule . /goimport.php [L]
</IfModule>
# END GolangRedirect

goimport.php

<?php 
   $importpath = rtrim(strtok($_SERVER["REQUEST_URI"],'?'), '/');
   $domain     = 'spirited.io';
   $githublink = 'https://gitlab.com/spirited';
   $goImport   = $domain . $importpath . ' git ' . $githublink . $importpath . '.git';
?>

<html>
<head>
   <meta name="go-import" content="<?= $goImport; ?>">
   <meta http-equiv="refresh" content="1;url=<?= $githublink . $importpath; ?>" />
</head>
</html>

Posted

Couldn't you just create a 301 redirect using a htaccess rule?

Yeah, that would be a better approach! Thanks for the suggestion. I'll look into doing that after work today.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...