-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreformat_urls.php
More file actions
174 lines (153 loc) · 7.77 KB
/
reformat_urls.php
File metadata and controls
174 lines (153 loc) · 7.77 KB
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
164
165
166
167
168
169
170
171
172
173
174
<?php
require_once("constants.php");
class ReformatUrls {
function __construct()
{
self::process_url_site();
}
public static function process_url_site() {
$debug = self::is_debug_active();
/*
if (in_array($_SERVER['REQUEST_URI'], URL_IGNORE_LIST)) {
return;
}
*/
/**
* Si la URL tiene el idioma antes que el sitio, la dejamos como está,
* pero sobrescribimos la variable de entorno $_SERVER["REQUEST_URI"] para que WPML la interprete correctamente.
*
* Si el idioma va detrás del sitio, redireccionamos con un 301 a la URL modificada (con el idioma delante del sitio).
*
* Sabemos qué fragmento de la ruta corresponde al idioma porque tiene dos carateres de largo.
*/
$url_path = $_SERVER["REQUEST_URI"];
$path_parts = explode("/",ltrim(parse_url($url_path,PHP_URL_PATH),"/"));
$is_admin = strpos($url_path, "wp-admin");
$is_login = strpos($url_path, WP_LANG_URL_CONFIG["login_directory"]);
$is_wp_json = strpos($url_path, "wp-json");
$path_is_hotel_and_lang = count($path_parts) > 1 && in_array($path_parts[0], WP_LANG_URL_CONFIG["slugs"]) && in_array($path_parts[1], WP_LANG_URL_CONFIG["langs"]);
$path_is_lang_and_hotel = count($path_parts) > 1 && in_array($path_parts[0], WP_LANG_URL_CONFIG["langs"]) && in_array($path_parts[1],WP_LANG_URL_CONFIG["slugs"]);
$path_is_only_hotel = count($path_parts) == 1 && in_array($path_parts[0], WP_LANG_URL_CONFIG["slugs"]);
$path_is_only_lang = count($path_parts) == 1 && in_array($path_parts[0], WP_LANG_URL_CONFIG["langs"]);
$path_is_lang_but_not_hotel = count($path_parts) > 1 && in_array($path_parts[0], WP_LANG_URL_CONFIG["langs"]) && !in_array($path_parts[1],WP_LANG_URL_CONFIG["slugs"]);
$path_is_not_lang_not_hotel = count($path_parts) == 1 && !in_array($path_parts[0], WP_LANG_URL_CONFIG["langs"]) && !in_array($path_parts[0], WP_LANG_URL_CONFIG["slugs"]);
if($debug){
print_r([
"url_path"=>$url_path,
"path_parts"=>$path_parts,
"path_is_hotel_and_lang"=>$path_is_hotel_and_lang,
"path_is_lang_and_hotel"=>$path_is_lang_and_hotel,
"path_is_only_hotel"=>$path_is_only_hotel,
"path_is_only_lang"=>$path_is_only_lang,
"path_is_not_lang_not_hotel" =>$path_is_not_lang_not_hotel,
"path_is_lang_but_not_hotel" =>$path_is_lang_but_not_hotel,
]);
}
if (!$is_admin && !$is_login && !$is_wp_json) {
if($path_is_only_hotel) {
/**
* Si no hay idioma incluido en la url
* Incluimos idioma por defecto y redireccionamos a esta con formato /YY/XXXX...
*/
array_unshift($path_parts, WP_LANG_URL_CONFIG["langs"][0]);
$path_prefix = "/" . $path_parts[0] . "/" . $path_parts[1];
$new_url_path = $path_prefix . '/' . substr($url_path, strlen($path_prefix) - 2);
$complete_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$new_url_path";
if($debug){
echo "path_is_only_hotel redirect to → ". rtrim($complete_url, "/");
exit();
}
header("Location: " . rtrim($complete_url, "/"), true, 301);
exit();
} elseif($path_is_hotel_and_lang) {
self::redirect_to_lang_hotel($path_parts, $url_path, $debug);
} elseif ($path_is_lang_and_hotel) {
/**
* Si la variable de entorno $_SERVER["REQUEST_URI"] tiene el formato /XX/YYYY...
* la sobrescribimos para que WP crea que es /YYYY/XX...
*/
$path_prefix = "/" . $path_parts[1] . "/" . $path_parts[0];
$path_tail = (strlen($url_path) == strlen($path_prefix)) ? "/" : substr($url_path, strlen($path_prefix));
# Si es el path raiz (no es post ni página) se le añade "/" al final, para que WP no lo redireccione
# Si no, se le añade el resto del path original /el post, página, o lo que sea)
// Add slash to not query part url
$path_tail_parts = explode("?", $path_tail,2);
$_SERVER["REQUEST_URI"] = $path_prefix . rtrim($path_tail_parts[0], "/")."/";
// If hast query string add it
if(count($path_tail_parts)>1){
$_SERVER["REQUEST_URI"] .="?".$path_tail_parts[1];
}
// $_SERVER["REQUEST_URI"] = rtrim($_SERVER["REQUEST_URI"] , "/");
if($debug){
echo "path_is_lang_and_hotel set REQUEST_URI → ". $_SERVER["REQUEST_URI"]."<br/>";
}
} elseif ($path_is_lang_but_not_hotel) {
//$_SERVER["REQUEST_URI"] = rtrim($_SERVER["REQUEST_URI"], "/")."/";
} elseif ($path_is_only_lang) {
//$_SERVER["REQUEST_URI"] = rtrim($_SERVER["REQUEST_URI"], "/")."/";
}
# Si contiene /? y el metodo es GET, se le quita esa barra
if ($_SERVER['REQUEST_METHOD'] === 'GET' && strpos($url_path, "/?") !== false) {
if($debug){
echo "remove end slash /? redirect to → ". str_replace("/?", "?", $url_path);
exit();
}
header("Location: " . str_replace("/?", "?", $url_path), true, 301);
exit();
}
# En cualquier caso, si acaba en / y el metodo es GET
if ($_SERVER['REQUEST_METHOD'] === 'GET' && str_ends_with($url_path, "/")) {
if($debug){
echo "remove end slash / redirect to → ". rtrim($url_path, "/");
exit();
}
header("Location: " . rtrim($url_path, "/"), true, 301);
exit();
}
if($debug){
echo "request: " . $_SERVER["REQUEST_URI"] . "<br>";
echo "Not redirect, go to WP";
}
}
if($debug){
echo "<br>Is admin, login or wp_json:";
if ($is_admin) {
echo "is admin<br>";
}
if ($is_login) {
echo "is login <br>";
}
if ($is_wp_json) {
echo "is wp_json<br>";
}
echo "request: " . $_SERVER["REQUEST_URI"] . "<br>";
echo "Not redirect, go to WP";
}
}
private static function redirect_to_lang_hotel(array $path_parts, mixed $url_path,bool $debug): void
{
/**
* Solo en GET:
* Si la variable de entorno $_SERVER["REQUEST_URI"] tiene el formato /XXXX/YY...
* con YY de dos carateres de largo (porque YY es el idioma)
* redireccionamos con un 301 a la URL correcta, que debe ser del formato /YY/XXXX...
*/
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$path_prefix = "/" . $path_parts[1] . "/" . $path_parts[0];
$new_url_path = $path_prefix . substr($url_path, strlen($path_prefix));
$complete_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$new_url_path";
if($debug){
echo "path_is_hotel_and_lang redirect to → ". rtrim($complete_url, "/");
exit();
}
header("Location: " . rtrim($complete_url, "/"), true, 301);
exit();
}
}
private static function is_debug_active(): bool
{
// return true;
return !empty($_GET['DEBUG6839']);
}
}
new ReformatUrls();