Wordpress permalink redirect rule - wordpress

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

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! :)

Alter GET variable in Wordpress

I am working on a wordpress self-hosted website, with the standard .htaccess settings:
In the website, I have a page called "animalpage". Using my rewrite settings, it is shown in the address bar as http://www.example.com/animalpage.
I am using a custom page template and am processing some things, which include the use of GET variables. For instance:
if (isset($_GET=['word'])) { echo $_GET['word]; } So, http://www.example.com/animalpage?word=cat will display "cat".
The problem I have is rewriting the URL so that it can look like: http://www.example.com/animalpage/dog, still being able to access "dog" as the GET variable.
I am not too good with mod_rewrite rules to begin with, but working from within a wordpress installation is throwing me an extra curveball.
Does anyone know what I need to add to my .htaccess to achieve this?
Thank you!
Old version:
RewriteRule ^animalpage/([a-z0-9]+)/?$ animalpage?word=$1 [NC,QSA]
Revisited:
RewriteRule ^animalpage/([^/])/?$ animalpage?word=$1 [NC,L,QSA]

How to fix 301 permalink redirect for funky structure

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.

How to use .htaccess to ignore/remove /page/X/ from url?

How can I use .htaccess to strip/ignore /page/X/ where X is the page number, from my urls, ONLY if it's right after the domain.com, like domain.com/page/29/ ?
Scraper sites are linking to my site with pagination on posts for some reason, causing Google to crawl those links. Unfortunately my site is not showing a 404, but instead showing a page, and the canonical url is including /page/29/ for example. It's not good. Hurting my rankings.
How would this be done in htaccess, so that pagination after anything else like a /page-name/ would still work?
You can try the following rule if you want to rewrite /page/X/ to /page/ exactly. (where X is any number)
RewriteRule ^page/([0-9]+)/$ http://domain.com/page/ [R=302,L]
If you want to rewrite /[anystring]/X/ to /[anystring]/ you can try
RewriteRule ^(.*)/([0-9]+)/$ http://domain.com/$1/ [R=302,L]

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