Why this snippet helpful?
- There are few reasons why we can tell this snippet is useful and the most common one is to avoid Google indexing the short link URL instead of the user friendly and that affect site ranking.
 Here’s a sample
 XHTML
1
2
3
4
5
// friendly url
http://www.mydomain.com/hello-world/
// short link url
http://www.mydomain.com/?p=2
 So if some one trying to access the short url they’re automatically redirected into 404 page or your site home-page.
1
2
3
4
5
6
7
8
9
10
// add this code in your theme functions.php file
function short_link_template_redirect()
{
    if(isset($_GET['p']) && $_GET['p']!=”) {
    {
        wp_redirect( home_url( ‘/404/’ ) ); // remove ‘/404/‘ if you want it to redirect to site home-page
        exit();
    }
}
add_action( ‘template_redirect’, ‘short_link_template_redirect’, 8 );

0 comments:

Post a Comment

 
Top