我在Perl中到处寻找使URL标准化的方法。换句话说,我正在尝试找到一种方法来检查,然后将example.org、www.example.org或http://example.org格式的网址更改为标准的http://www.example.org格式,而不管域名是什么。任何建议都将不胜感激。
发布于 2015-07-27 12:50:05
你可以自己标准化它,如下所示:
#!/usr/bin/env perl
my $host = 'example.org';
my $canonical = 'http://www.example.org';
for ( 'example.org',
'www.example.org',
'http://example.org',
'example.org/foo/bar?baz=123',
'www.example.org/foo/bar?baz=123',
'http://example.org/foo/bar?baz=123',
) {
# Anything followed by 'example.org' followed by anything
my ($path) = m|^.*?$host(.*)$|;
my $canonical_path = join '', $canonical, $path || '';
print sprintf("% 40s => %s\n", $_, $canonical_path);
}以下哪项输出:
example.org => http://www.example.org
www.example.org => http://www.example.org
http://example.org => http://www.example.org
example.org/foo/bar?baz=123 => http://www.example.org/foo/bar?baz=123
www.example.org/foo/bar?baz=123 => http://www.example.org/foo/bar?baz=123
http://example.org/foo/bar?baz=123 => http://www.example.org/foo/bar?baz=123发布于 2015-07-27 12:56:27
您可以将以下内容放入每个网站的.htaccess文件中。
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]它将把(301)非www域重定向到www。
https://stackoverflow.com/questions/31644197
复制相似问题