How to fix 301 permalink redirect for funky structure - permalinks

I'm trying to figure out how to redirect the permalink structure for some of my very old WordPress news portals, which are using funky structures that were needed for Google News and old search engines, but now are crap.
For example, I currently have:
mynewssite.xcom/2015/03/19/fnw14770_161419.php
Where I'd rather have:
mynewssite.xcom/pretty-nameo-of-the-article-fnw14770/
I would even be okay with this instead:
mynewssite.xcom/pretty-name-of-the-article-fnw14770_161419.php
My current WordPress permalink structure is:
/%year%/%monthnum%/%day%/fnw%post_id%_%hour%%minute%%second%.php
I need to do a 301 redirect to something like /%postname%/ or /%postname%-%post_id%/, or in a worst case senario, something like this:
%postname%-fnw%post_id%_%hour%%minute%%second%.php
I don't know how to write rewrite expressions. This is what I currently have now, but it's not working:
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/fnw(\d+)_%hour%%minute%%second%.php$ http://floridanewswire.com/?p=$4

%hour%%minute%%second% won't work in .htaccess, as this is meta information known only to WordPress. Instead, try replacing it with a rule that matches six digits, such as \d{6}
Also, you should remove unnecessary capture groups, escape forward sashes and dots, and use \d instead of [0-9].
Here is a fixed and simplified version of your expression:
RedirectMatch 301 ^\/\d{4}\/\d{2}\/\d{2}\/fnw(\d+)_\d{6}\.php http://floridanewswire.com/?p=$1
You should be able to change your permalinks to a cleaner structure on the Settings > Permalinks admin menu and have the old permalinks redirected through this htaccess rule.

Related

how to modified wordpress category url like www.example.com/m/a1 to www.example.com/category/m/a1

i have wordpress site .I config category urls to not contain world "category" but some later i change it .
now in google i have both www.example.com/m/a1 and www.example.com/category/m/a1
and so i get not found error for www.example.com/m/a1 because it changed to www.example.com/category/m/a1
Is any way to redirect All Url Format like www.example.com/m/a1 to www.example.com/category/m/a1 without loss none of 2 urls?
You can redirect all those pages by defining htaccess rule like this:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^m/(.*)$ /category/m/$1 [L,NC,R=302]
This will redirect all www.example.com/m/anypage to www.example.com/category/m/anypage
PS: Please be careful while using these rules, small mistake can crash the site.
Hope it helps! :)

htaccess redirect url with special characters (+)

I have a URL like this.
http://olddomain.com/product-category/x-tended+bottoms/
and I wanted to apply a 301 redirect via htaccess to http://newdomain.com/shop/extended-bottoms
However, when i'm trying to apply a rule that goes like this, it doesn't take effect and instead lands me to page not found.
RedirectMatch 301 ^/product-category/x-tended+bottoms/(.*)$ https://mothersenvogue.com.hk/shop/all-bottoms?
What regex can I use to change the plus sign (+) on the url?
So I was able to find a reference answer to my question by adding the highlighted rule to check the plus sign (+) on URLs:
RedirectMatch 301 ^/product-category/x-tended**[\s+]**bottoms/$ https://newdomain.com/?$
htaccess redirect a plus sign

htaccess redirect folders with specific word to their own parent folder

New site is live and the old site made use of a bunch of useless "feeds".
Looking for an .httaccess 301 redirect command to redirect anything with "feed" in it to its parent folder. There are thousands of them, so looking for a way to do it in one fell swoop.
Here are examples of the "folders" (which are really WordPress pages):
http://example.com/listing/listing-name/feed/
http://example.com/listing/listing-name2/feed/
Where "listing" always remains the same, but "listing-name" and "listing-name2" represents items that vary.
I would like them to be redirected to their parent "folder", like this:
http://example.com/listing/listing-name/
http://example.com/listing/listing-name2/
Any help would be greatly appreciated.
Thank you!
You can use this redirect rule as your top rule:
RewriteEngine On
RewriteRule ^(.+/)feed/?$ /$1 [L,NC,NE,R=301]
# more rules below this

Wordpress permalink redirect rule

Running into a little bit of an issue with Wordpress permalink redirects that I was hoping I could get some help with.
Previously, I utilized the following permalink structure for my blog posts:
/blog/%year%/%monthnum%/ %day%/%postname%/
I've recently changed it to:
/blog/%postname%/
Problem is, none of my old links which were structured using the old format now work!
I know it's possible to write a general .htaccess 301 redirect rule, but besides knowing that it's possible, I don't actually know how to do it.
Any tips?
This will strip out any number/number/number/ formatting from the url
RewriteRule ^blog/([0-9]+/){3}(.*) /blog/$2 [R=301,L]
WordPress should recognize the old permalinks by default. You should try hitting Dashboard > Settings > Permalinks > Save changes one more time.
In case it doesn't work, the regex your're asking for would be something like this, removing 4 digits, a slash, 2 more digits, another slash, yet 2 more digits and one last slash from the URL:
RewriteEngine On
RewriteRule ^blog/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$ http://example.com/blog/$1

Redirect joomla url's to wordpress

I moved an ex-site based on joomla to wordpress. Import worked fine but the problem is that the old links don't work anymore.
Because there is only 50 or so articles, i thought will be a good idea to put a rule for each post (in .htaccess).
Well... Not always things are like you want, so redirects dont work at all :(
Old joomla links looks like this:
http://site.com/index.php?option=com_content&task=view&id=49&Itemid=29
http://site.com/index.php?option=com_content&task=view&id=42&Itemid=29
http://site.com/index.php?option=com_content&task=view&id=68&Itemid=29
And need to be translated to:
http://site.com/?p=23
http://site.com/?p=24
http://site.com/?p=25
basically no relations between old and new links, so i don't think a regex will help
both old and new site are on the same domain
Ok, the problem is that any rule i've tried (and i tried a LOT!), none worked. in few cases i get 500 error, but most of times the redirect didn't work.
So, any of you guys had same problem? I don't necessary want to have nice permalinks, but if i can, that will be better. The problem is that i have many backlinks to old url's and i don't want to loose them.
Thanks a lot guys!
Since the conversion of your site over to Wordpress is relatively new, is there anything preventing you from using the old Joomla! ID's in your WP database table? This would allow you to use a regex fairly easily.
Another option would be to create a separate PHP script that handles the Joomla! URLs then redirects to the Wordpress ones. So you would have a regex in your Apache configuration detecting index.php?option=com_content&task=view URLs, finding the value for 'id', then redirecting to someotherscript.php that would have a map of your ids from Joomla! to Wordpress. This script would then use header('Location: ?p=' . $id) to redirect to the correct page in Wordpress.
Thnaks for the idea! I put this in index.php (wordpres default):
if(isset($_GET['option'])) {
if(is_numeric($_GET['id'])){
header ('HTTP/1.1 301 Moved Permanently');
header("Location: http://www.site.com/?p={$_GET['id']}");
die();
}else {
die('Hacking attempt');
}
}
And works like... GREAT! :D
Another option might have been to use a redirection plugin to do this for you. Saves the solution breaking each time you change or update your theme.
I had a very similar issue with some unknown CMS to Joomla.
If you want to do it with .htaccess in Apache there is a way, but if there is absolute no relation between the old URL and the new URL than you have to write two lines for each URL pair.
RewriteEngine On
# now the first Example
RewriteCond %{QUERY_STRING} ^option=com_content&task=view&id=49&Itemid=29$
RewriteRule ^index\.php$ /?p=23 [R=301,L]
# Repeat last two lines for all your URLs
I'm not sure if you really have this kind of new URLs. Personally a SEF URL would be better e.g.: https://example.com/path/to/new/page
If you want to do this than you can do it, but you have to add a ? at the end of the destination otherwise the old Query string would be added to you new destination like this: https://example.com/path/to/new/page?option=com_content&task=view&id=49&Itemid=29
so for this example do it as follows:
RewriteCond %{QUERY_STRING} ^option=com_content&task=view&id=49&Itemid=29$
RewriteRule ^index\.php$ /path/to/new/page? [R=301,L]

Resources