| 1 |
<?php
|
| 2 |
//$Id: globalredirect.module,v 1.4 2006/11/14 17:23:27 njt1982 Exp $
|
| 3 |
/**
|
| 4 |
* Implementation of hook_help().
|
| 5 |
*/
|
| 6 |
function globalredirect_help($section) {
|
| 7 |
switch ($section) {
|
| 8 |
case 'admin/modules#description':
|
| 9 |
return t('Searches for an alias of the current URL and redirects if found. Stops duplicate content arising when path module is enabled.');
|
| 10 |
}
|
| 11 |
}
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_init().
|
| 15 |
*/
|
| 16 |
function globalredirect_init() {
|
| 17 |
if(isset($_REQUEST['q']) && function_exists('drupal_get_path_alias')) {
|
| 18 |
//Check the current url (eg, node/123) for an alias... If set, redirect to it
|
| 19 |
$alias = drupal_get_path_alias($_REQUEST['q']);
|
| 20 |
if ($alias != $_REQUEST['q']) {
|
| 21 |
drupal_set_header("HTTP/1.1 301 Moved Permanently");
|
| 22 |
drupal_goto($alias);
|
| 23 |
exit();
|
| 24 |
}
|
| 25 |
|
| 26 |
//Check if the current url ends in a slash (eg, node/123/)
|
| 27 |
if(substr($_REQUEST['q'], -1) == "/") {
|
| 28 |
//Ok - it does. Seeing as it didn't have an exact match WITH the slash in the above test, lets try without...
|
| 29 |
$alias = drupal_get_path_alias(substr($_REQUEST['q'], 0, -1));
|
| 30 |
if ($alias != $_REQUEST['q']) {
|
| 31 |
//We matched an alias for the current URL with no slash - redirect to that...
|
| 32 |
drupal_set_header("HTTP/1.1 301 Moved Permanently");
|
| 33 |
drupal_goto($alias);
|
| 34 |
exit();
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
|
| 39 |
//Nothing above has been matched - lets do a check to compare the current url to the site's frontpage.
|
| 40 |
// If matches, redirect to the frontpage (eg, http://www.mysite.com/)
|
| 41 |
$frontpage = variable_get('site_frontpage', '/');
|
| 42 |
if($_REQUEST['q'] == $frontpage) {
|
| 43 |
drupal_set_header("HTTP/1.1 301 Moved Permanently");
|
| 44 |
drupal_goto('');
|
| 45 |
exit();
|
| 46 |
}
|
| 47 |
}
|
| 48 |
}
|