Avatar of catonthecouchproductions
catonthecouchproductions
Flag for United States of America asked on

WordPress 301 Redirects - Batch - Match a pattern of parent/sub pages

Hey,

I am restructuring a client site and I have this:

New: http://www.brookline.com/living-in-brookline-ma/shopping/shopping-at-chestnut-hill/

Old: http://www.brookline.com/shopping/shopping-at-chestnut-hill/

So for all shopping we add:

- living-in-brookline
- shopping

Is there an easy way to match a Rewrite rule to fix all these sections for I dont have to go through each one?

Thanks,

Ryan
WordPressApache Web Server

Avatar of undefined
Last Comment
Jason C. Levine

8/22/2022 - Mon
Jason C. Levine

catonthecouchproductions

ASKER
Yes, but I have various groups of pages that I put in to a new parent page, and I am trying to find a way to do a batch 301 redirects but using a wildcard.

I searched on google for various fixes and nothing really fit what I needed. The custom permalink plugin worked great on my single pages though!
ASKER CERTIFIED SOLUTION
Jason C. Levine

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
catonthecouchproductions

ASKER
Ah great! If this is on a development server under: /brookline/ - Where would I add that folder, similar to how WP does this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /brookline/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /brookline/index.php [L]
</IfModule>
Your help has saved me hundreds of hours of internet surfing.
fblack61
catonthecouchproductions

ASKER
I tried this:

RewriteRule ^(.*)/(.*)$ /brookline/$1/living-in-brookline-ma/$2 [R=301,L]

But it wont load any assets, its adding in living-in-brookline-ma to every URL, thoughts?

i was thinking if it had that living-in-brookline-ma in the tag it would match the rest or the rule above matches ALL cases?

i would have this too:

RewriteRule ^(.*)/(.*)$ /brookline/$1/neighborhoods/$2 [R=301,L]
RewriteRule ^(.*)/(.*)$ /brookline/$1/living-in-brookline-ma/$2 [R=301,L]

etc

Thanks, I think its close!

Ryan
catonthecouchproductions

ASKER
I think I got it working:

RewriteRule ^visitors-center/the-arts/[A-Za-z]/$ /living-in-brookline-ma/the-arts/$1/ [R=301,L]

Now a slight catch, when i have Custom Permalinks activated, my rules in my .htaccess wont work? Any thoughts on how to make them play nice?
catonthecouchproductions

ASKER
Jason, you wouldnt know how that Custom Permalinks plugin works? It doesnt add anything in to the htaccess.

If you read my note above it doesnt play well with that rewrite above.

Do you know if I can make a custom permalink without the plugin for specific pages?

Ryan
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jason C. Levine

I would have to pull that plugin apart to see what it does but my guess is that it is doing PHP header() functions and adding a redirect that way.  So the flow looks like:

user enters URL
WordPress .htaccess translates URL to ID number, fetches page
Custom Permalinks fires, adds new 301 redirect via header()
Browser displays content with Custom Permalinks header

Again, purely speculation as I would have to download the plugin and deconstruct it.
catonthecouchproductions

ASKER
Ah, I see. That does make sense.

So I wonder what is the best way to still use that plugin and do the rewrite above?

Ryan
Jason C. Levine

I was almost right.  The plugin uses a WordPress function to do its magic:

wp_redirect( get_home_url()."/".$url, 301 );

So this is definitely activating after the .htaccess file is processed.

This plugin is only useful for assigning custom permalinks for individual posts, pages, tags or categories. It will not apply whole permalink structures, or automatically apply a category's custom permalink to the posts within that category.

The plugin probably isn't intended to work in conjunction with manual rewrites because it fires after that.  So either you specify the redirects in .htaccess or in the plugin but not both.

It's worth noting that if you can write regular expressions and the links to be redirected fit any sort of pattern, then you can handle this in .htaccess without the need for the plugin.

Do you know if I can make a custom permalink without the plugin for specific pages?

Not easily.  You would probably have to switch to custom taxonomies to get the different slugs you desire.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
catonthecouchproductions

ASKER
Thanks jason!

See this image: http://cl.ly/163E460C1w3i3w322o2w - would something like that work with:

RewriteRule ^shopping/[A-Za-z]/$ /living-in-brookline-ma/shopping/$1/ [R=301,L]

I tried some variations just now and didnt seem to work, any ideas?
Jason C. Levine

The rule above is not correct to use a backreference (the $1).

Should be:

ewriteRule ^shopping/([A-Za-z])/$ /living-in-brookline-ma/shopping/$1/ [R=301,L]
catonthecouchproductions

ASKER
Ah, so you added ()?

Did you check out that image above? I am trying to see if I can use that plugin for redirects, they accept regex but I cant seem to get it to work? I know that Rewrite above needs to be modified a bit to work with the Redirection plugin.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jason C. Levine

I did check out the screenshot but I'm not all that clear on what that plugin expects and if it works properly or not.  My guess is the source URL would be something like:

http://yoursite.com/shopping/([A-Za-z])/

and target would be

http://yoursite.com/living-in-brookline-ma/shopping/$1/