Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

Redirect via WordPress

Hello all...

I have a WordPress page that is: https://mydomain.com/some-wordy-page-url/

How can I create a thing that displays https://mydomain.com/here/

Any help would be appreciated.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

There are plugins that do this. However, I have had issues with these where they were the source of a hack.

Now I will do this in my functions.php page on my child theme.

if($_SERVER['REQUEST_URI']=="/some-wordy-page-url/")
{
    wp_redirect( "https://".$_SERVER['SERVER_NAME']."/here/", 301 );
    exit;
}

Open in new window


You can just use the full https://mydomain.com/here/  on the redirect. I use that bit of code so I can easily switch between the development site and live site.

The code ads a 301 redirect which tells google and other search engines that some-wordy-page-url no longer exists and you should instead look /here/
Avatar of Sheldon Livingston

ASKER

Thanks Scott... the intent was to keep the wordy page but just have /here/ point to it... so wordy will still exist. Is this an issue?
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You said, "the intent was to keep the wordy page but just have /here/ point to it... so wordy will still exist. Is this an issue?"

If I understand what you're asking, you'll switch the sense of the code Scott provided you.

Also, best to use a 302, rather than 301, because if you use a 301 any changes you make to redirect target link will only be seen by new visitors + returning visitors will always go to old target link.

This occurs because 301s cache in browsers forever, making it impossible to change once a person visits a link.
Alternatively you can place a 302 redirect in your .htaccess file too.

Something like this...

RewriteRule ^some-wordy-page-url/?$ https://mydomain.com/here/?%{QUERY_STRING} [L,R=302]

Open in new window

For clarification and in simple terms, the 301 is for permanent changes where you are telling users, I no longer wish to use /old-uri/ and to only use /new-uri/.  The 302 means temporary and should only be used for things like testing multiple versions of a page or maybe you temporarily want to send users to an under construction page.

David, I think genericly saying it is best to use a 302 can be misleading. 
It's worth mentioning that if you install the Redirections plugin, then when there are changes to Page permalinks WordPress will automatically redirect traffic from the old URL to the new URL. You may need to configure monitoring of pages and posts in the plugin Options page before it will do that, but it's not difficult.

The plugin is free from the WP Plugin repository:
https://wordpress.org/plugins/redirection/
I'd agree with Scott about his 301/302 discussion for site owners with high technical expertise.

Practically though... most site owners have low technical expertise...

I've taken on many projects with all sorts of analytics/conversions/cashflow problems where some low tech site owner has been changing the terminal link of some 301 redirect... for years... to reflect many terminal links...

The result is visitors landing on sometimes 20X+ different pages. This type of mess is incredibly difficult to understand + repair, also extremely costly to fix.

Trying to explain to site owners (and the various low paid, low competence people they hire for site work) the difference between a 301 + 302 is nearly impossible.

One approach taken by many site owners, once they understand this problem, is to install a Must Use Plugin (mu-plugins directory), which forcibly converts all 30Xs to 302s, so site owners can let low paid + low competence developers on their site without trying to train them about the difference.

Using a Must Use Plugin promoting all 30X types to 302 is the answer for sites with many admin users.