We help IT Professionals succeed at work.
Troubleshooting Question

Redirect via WordPress

45 Views
Last Modified: 2020-09-24
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.
Comment
Watch Question

Scott FellDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
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;
}


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/
Sheldon LivingstonConsultant

Author

Commented:
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?
Developer
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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

Scott FellDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
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. 
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
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/
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.