forked from virtualmin/virtualmin-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-redirect.pl
executable file
·164 lines (148 loc) · 4.55 KB
/
create-redirect.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/local/bin/perl
=head1 create-redirect.pl
Adds a web redirect or alias to some domain
A redirect maps some URL path like /foo to either a different URL, or a
different directory on the same virtual server. This can be used to provide
more friendly URL paths on your website, or to cope with the movement of
web pages to new locations.
This command takes a mandatory C<--domain> parameter, followed by a virtual
server's domain name. The C<--path> parameter is also mandatory, and must be
followed by a local URL path like C</rails> or even C</>.
To redirect to a different URL, use the C<--redirect> flag followed by a
complete URL starting with http or https, or a URL path on this same domain.
To map the path to a directory, use the C<--alias> flag followed by a full
directory path, ideally under the domain's C<public_html> directory.
By default, requests for sub-paths under the URL path will be mapped to
the same sub-path in the destination directory or path. However, you can
use the C<--exact> flag to only match the given path, or the C<--regexp> flag
to ignore sub-paths when redirecting.
For domains with both non-SSL and SSL websites, you can use the C<--http> and
C<--https> flags to limit the alias or redirect to one website type or the
other.
To set a custom HTTP status code for the redirect, you can use the C<--code>
flag followed by a number. Otherwise the default code of 301 (temporary
redirect) will be used.
=cut
package virtual_server;
if (!$module_name) {
$main::no_acl_check++;
$ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
$ENV{'WEBMIN_VAR'} ||= "/var/webmin";
if ($0 =~ /^(.*)\/[^\/]+$/) {
chdir($pwd = $1);
}
else {
chop($pwd = `pwd`);
}
$0 = "$pwd/create-redirect.pl";
require './virtual-server-lib.pl';
$< == 0 || die "create-redirect.pl must be run as root";
}
@OLDARGV = @ARGV;
# Parse command-line args
&require_mail();
while(@ARGV > 0) {
local $a = shift(@ARGV);
if ($a eq "--domain") {
$domain = shift(@ARGV);
}
elsif ($a eq "--path") {
$path = shift(@ARGV);
}
elsif ($a eq "--redirect") {
$url = shift(@ARGV);
}
elsif ($a eq "--alias") {
$dir = shift(@ARGV);
}
elsif ($a eq "--regexp") {
$regexp = 1;
}
elsif ($a eq "--exact") {
$exact = 1;
}
elsif ($a eq "--multiline") {
$multiline = 1;
}
elsif ($a eq "--http") {
$http = 1;
}
elsif ($a eq "--https") {
$https = 1;
}
elsif ($a eq "--code") {
$code = shift(@ARGV);
$code =~ /^\d{3}$/ && $code >= 300 && $code < 400 ||
&usage("--code must be followed by a HTTP status code between 300 and 400");
}
elsif ($a eq "--help") {
&usage();
}
else {
&usage("Unknown parameter $a");
}
}
$domain || &usage("No domain specified");
$path || &usage("No redirect path specified");
$exact && $regexp && &usage("Only one of --regexp or --exact can be given");
if ($url) {
$url =~ /^*(http|https):\/\/\S+$/ ||
$url =~ /^\/\S+$/ ||
&usage("The --redirect flag must be followed by a URL or ".
"a URL path");
}
elsif ($dir) {
$dir =~ /^\/\S+$/ && -d $dir ||
&usage("The --alias flag must be followed by a directory");
}
else {
&usage("One of --redirect or --alias must be given");
}
if (!$http && !$https) {
# If no protocol was given, assume both for backwards compatability
$http = $https = 1;
}
$d = &get_domain_by("dom", $domain);
$d || usage("Virtual server $domain does not exist");
&has_web_redirects($d) ||
&usage("Virtual server $domain does not support redirects");
# Check for clash
&obtain_lock_web($d);
@redirects = &list_redirects($d);
($clash) = grep { $_->{'path'} eq $path } @balancers;
$clash && &usage("A redirect for the path $path already exists");
# Create it
$r = { 'path' => $path,
'dest' => $url || $dir,
'alias' => $dir ? 1 : 0,
'regexp' => $regexp,
'exact' => $exact,
'http' => $http,
'https' => $https,
'code' => $code,
};
$err = &create_redirect($d, $r);
&release_lock_web($d);
if ($err) {
print "Failed to create redirect : $err\n";
exit(1);
}
else {
&set_all_null_print();
&run_post_actions();
&virtualmin_api_log(\@OLDARGV, $d);
print "Redirect for $path created successfully\n";
}
sub usage
{
print "$_[0]\n\n" if ($_[0]);
print "Adds redirect or alias to a virtual server's website.\n";
print "\n";
print "virtualmin create-redirect --domain domain.name\n";
print " --path url-path\n";
print " --alias directory | --redirect url\n";
print " [--regexp | --exact]\n";
print " [--code number]\n";
print " [--http | --https]\n";
exit(1);
}