How to change rewrite links?
Implementation
I suggest you see the implementation in two stages: a first approach,
we will see how each mechanism and then we discuss the comprehensive
approach.
The reason for this separation is that we have control over the links contained within our own site, which means that we can change without the end user's knowledge. This allows us to gradually eliminate broken links.
However, the links outside our site are out of our reach. So we need to tell visitors that use this type of link it is recommended to update their favorites.
The reason for this separation is that we have control over the links contained within our own site, which means that we can change without the end user's knowledge. This allows us to gradually eliminate broken links.
However, the links outside our site are out of our reach. So we need to tell visitors that use this type of link it is recommended to update their favorites.
External links
This is done using a code HTTP/1.1: when a visitor uses an address we know bad, we need to use the code 301 ("moved permanently"). Then we show the visitor location of the correct address.
In practice, we must first find (restore) the source address rewriting and use our links to redirect to the correct address.
The first phase (recovery) is carried out using the. Htaccess file: we substitute the wrong address by a redirection script which we indicate the standard address (physical name of the script + parameters). In this way, it is sufficient to redirect script to rewrite this address according to the rewriting rules we defined in the previous tutorial.
Redirection from an incorrect address
Select
<?php
header ( ' HTTP/1.1
301
Moved
Permanently '
) ;
header ( ' Location:
nouvelle-adresse.ext '
) ;
exit ;
//
Il
ne
faut
rien
envoyer
d'autre
?>
The first phase (recovery) is carried out using the. Htaccess file: we substitute the wrong address by a redirection script which we indicate the standard address (physical name of the script + parameters). In this way, it is sufficient to redirect script to rewrite this address according to the rewriting rules we defined in the previous tutorial.
File:. Htaccess
Select
DirectoryIndex index.php Options +FollowSymlinks RewriteEngine on #Restauration des URLs de base RewriteRule ^ftopic([0-9]+).* /phpbb-test/redirect.php?url=viewtopic.php?t=$1 [L] RewriteRule ^forum([0-9]+).* /phpbb-test/redirect.php?url=viewforum.php?f=$1 [L] #Réécriture normale RewriteRule ^sujet-([0-9]+).* /phpbb-test/viewtopic.php?t=$1 [L] RewriteRule ^forum-([0-9]+).* /phpbb-test/viewforum.php?f=$1 [L]
We find in this file. Htaccess three types of URLs that we saw earlier in the Problem: URLs basic (viewtopic.php? T = $ 1), intermediate (ftopic [0-9] +. *) and final (subject-[0-9] +. *).
File: redirect.php
Select
<?php
define ( ' IN_PHPBB '
,
true ) ;
$ phpbb_root_path
=
' ./ '
;
include ( $ phpbb_root_path
.
' extension.inc '
) ;
include ( $ phpbb_root_path
.
' common. '
.
$ phpEx
) ;
if ( !
empty ( $ userdata
[
' user_lang '
]
)) {
$ current_language
=
$ userdata
[
' user_lang '
] ; }
else {
$ current_language
=
$ board_config
[
' default_lang '
] ; }
include ( $ phpbb_root_path
.
' /language/lang_ '
.
$ current_language
.
' /lang_urlrewrite. '
.
$ phpEx
) ;
if ( is_array ( $ lang
[
' urlrewrite '
]
)) {
require_once ( $ phpbb_root_path
.
' includes/functions_urlrewrite. '
.
$ phpEx
) ;
$ new_url
=
rewrite_url( urldecode ( $ _GET
[
' url '
]
) ,
$ lang
[
' urlrewrite '
]
) ;
header ( ' HTTP/1.1
301
Moved
Permanently '
) ;
header ( ' Location:
'
.
$ new_url
[
' url '
]
) ;
exit ; }
?>
The call to urldecode ()
is required by the scripts using several parameters (they are legion)
and a constraint imposed by our. Htaccess, in which we simulated a call
to urlencode ().
Why urldecode () is necessary:
- / Phpbb-test/redirection.php? Url = viewtopic.php? T = 1 & start = 25 ==> there is confusion for the parameter "start" here can be sent to the script "redirection.php" (it is not what we want)
- / Phpbb-test/redirection.php? Url = viewtopic.php? T = 1 & start = 25 ==> "start" is not yet included as a parameter but as part of the parameter "url" (c ' is what we want)
If our website has undergone several rewrites successive ties, just complete the "Restoring URLs basic" our. Htaccess file!
File:. Htaccess
Select
DirectoryIndex index.php Options +FollowSymlinks RewriteEngine on #Restauration des URLs de base RewriteRule ^ftopic([0-9]+).* /phpbb-test/redirect.php?url=viewtopic.php?t=$1 [L] RewriteRule ^forum([0-9]+).* /phpbb-test/redirect.php?url=viewforum.php?f=$1 [L] RewriteRule ^viewtopic.php?t=([0-9]+) /phpbb-test/redirect.php?url=viewtopic.php?t=$1 [L] RewriteRule ^viewforum.php?f=([0-9]+) /phpbb-test/redirect.php?url=viewforum.php?f=$1 [L] #Réécriture normale RewriteRule ^sujet-([0-9]+).* /phpbb-test/viewtopic.php?t=$1 [L] RewriteRule ^forum-([0-9]+).* /phpbb-test/viewforum.php?f=$1 [L]
Internal links
If we look at one of the language files from the other tutorial, we note the presence of keys to the array $ lang ['urlrewrite'] ['regexes']. We will use the same keys in our current table in order to match URLs without problem.
To restore the URLs, we need to address the "raw" in a separate table that we call $ raw_urls. We will use another table, $ rewritten_urls to contain different versions of each address rewritten: Index "old" addresses used built with older versions of rewriting links on our site. Each of the other indexes is a language installed in our forum phpBB: users could post addresses all these different languages, we must standardize the presentation.
In all these tables, we use the keys we noticed above.
Now we need to restore the default URLs for phpBB.
I chose to begin by restoring the default URLs and then leave it to rewrite our method to load the rest of the operations in the same way she used to.
Restore_urls now study the function ().
The principle is similar to the function rewrite_urls (): start by listing all the rewrite URLs and then all of a sudden deal with the function str_replace ().
To restore the URLs, we need to address the "raw" in a separate table that we call $ raw_urls. We will use another table, $ rewritten_urls to contain different versions of each address rewritten: Index "old" addresses used built with older versions of rewriting links on our site. Each of the other indexes is a language installed in our forum phpBB: users could post addresses all these different languages, we must standardize the presentation.
In all these tables, we use the keys we noticed above.
$ & $ Raw_urls rewritten_urls
Select
<?php
$ raw_urls
=
array ( ' viewtopic_topicid_title '
= >
' viewtopic.php?t=$1 '
,
' viewforum_id_title '
= >
' viewforum.php?f=$1 '
,
//
...
) ;
$ rewritten_urls
[
' old '
] =
array ( ' viewtopic_topicid_title '
= >
array ( ' ftopic([0-9]+) '
) ,
' viewforum_id_name '
= >
array ( ' forum([0-9]+) '
) ,
//
...
) ;
//
...
if ( $ directory
=
dir ( $ phpbb_root_path
.
' language '
)) {
while ( $ entry
=
$directory
->read
()) {
if ( is_file ( $ phpbb_root_path
.
' language/ '
.
$ entry
.
' /lang_urlrewrite. '
.
$ phpEx
)) {
include ( $ phpbb_root_path
.
' language/ '
.
$ entry
.
' /lang_urlrewrite. '
.
$ phpEx
) ;
$ rewritten_urls
[
basename ( $ entry
) ] =
$ lang
[
' urlrewrite '
] [
' regexes '
] ; } } }
?>
I chose to begin by restoring the default URLs and then leave it to rewrite our method to load the rest of the operations in the same way she used to.
A semantic point: in this case, "restore" means find a default URL for phpBB from a rewritten URL, while "rewrite" means to change the default URL in a rewritten URL. Both terms can be used in both situations, that is why I prefer to be clear.
To retrieve the URL of the page, I modified the code used in includes / page_tail.php:
Recovery of URLs to restore then rewrite (file: includes / page_tail.php)
Select
require_once ( ' functions_urlrewrite. '
.
$ phpEx
) ;
// //
Find
all
the
links
in
the
page //
There
are
4
parenthesized
parts
in
these
regular
expressions: //
#1:
there
might
be
an
option
like
class="..." //
#2:
full
path
to
the
target
Web
page //
#3:
#1
bis //
#4:
text
of
the
link
(optional) //
$ regexes
=
array ( ' a '
= >
' #<a(.+)href="([^"]+)"([^>]*)>(.*)</a>#Usi '
,
' form '
= >
' #<form(.+)action="([^"]+)"([^>]*)>#Usi '
,
' link
rel '
= >
' #<link
rel(.+)href="([^"]+)"([^>]*)>#Usi '
) ;
foreach ( $ regexes
as $ link_type
= >
$ pattern
) {
$ matches
=
array () ;
if ( $ link_type
= =
' a '
) {
//
Restore
only
anchor-type
links
if ( preg_match_all ( $ pattern
,
$ contents
,
$ matches
,
PREG_SET_ORDER)) {
$ contents
=
restore_urls( $ contents
,
$ matches
) ; } }
$ matches
=
array () ;
if ( preg_match_all ( $ pattern
,
$ contents
,
$ matches
,
PREG_SET_ORDER)) {
$ contents
=
rewrite_urls( $ contents
,
$ matches
,
$ link_type
) ; } }
Restore_urls now study the function ().
The principle is similar to the function rewrite_urls (): start by listing all the rewrite URLs and then all of a sudden deal with the function str_replace ().
Restore_urls function () in the file includes / functions_urlrewrite.php
Select
/* *
Restore
URLs
to
their
original
(not
rewrited)
state *
it
builds
a
string
suitable
for
URLs *
@param
string
$string
The
string
to
rewrite *
@global
array
$lang
The
language
variables *
@return
string
The
rewritten
string */
function restore_urls($ contents
,
$ matches
){
global$ raw_urls
,
$ rewritten_urls
;
$ patterns
=
array ();
//
The
link
patterns
will
be
added
to
this
array
$ replacements
=
array ();
//
The
link
replacements
will
be
added
to
this
array
foreach ($ matches
as$ match
){
$ current_url
=
trim ($ match
[
2]
);
if ( in_array ($ current_url
,
$ patterns
)){
continue;
//
Skip
this
match
}
foreach ($ rewritten_urls
as$ pool
){
// //
Old
and
new
rewriting
(handled
as
a
pool
of
URLs) //
foreach ($ pool
as$ key
= >
$ regexes
){
// //
Each
rewrited
URL
in
this
URL
pool
(usually:
one) //
These
URLs
should
be
available
in
a
generic
form //
using
a
regular
expression
syntax
(without
delimiters) //
foreach ($ regexes
as$ key
= >
$ regex
){
$ regex
=
' / '
.
$ regex
.
' / '
;
if ( preg_match ($ regex
,
$ current_url
)){
$ changed_url
=
preg_replace ($ regex
,
$ raw_urls
[
$ key
] ,
$ current_url
);
$ patterns
[ ] =
' href=" '
.
$ current_url
.
' " '
;
$ replacements
[ ] =
' href=" '
.
$ changed_url
.
' " '
;
break 2; } } } } }
return str_replace ($ patterns
,
$ replacements
,
$ contents
); }
0 comments:
Post a Comment