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

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]

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

Mod Rewrite Rule to take article ID from URL and place a Slash before it

I have a wordpress site which has hundreds of articles with URLs in the following format:
http://www.example.com/news/variable-article-name-XXXXXXXXX/
where XXXXXXXXX is always a 9 digit number
I'd like to rewrite the URLs to include a slash before the number; to appear as follows.
http://www.example.com/news/variable-article-name/XXXXXXXXX/
Is there a simple, single rule I can write to achieve this?
Any help appreciated.
Try the following:
RewriteRule ^news/(.*)-(\d{9})/?$ /news/$1/$2 [R=301,L]
You'd still need some internal rewrite so that the redirected URL can be forwarded to appropriate pages.

.htacces rewrite rule with pagination

A clients wordpress site requires some rewrite rules due to some issues with a theme they are using and the hosting setup, currently I have one which is:
RewriteRule ^events index.php?pagename=events [L]
So when I go to www.site.com/events it'll show me www.site.com/index.php?pagename=events
This works fine, however the theme they are using on their events page will show pagination links, which produces links like:
www.site.com/events/?page_id_all=2
Which because of the previous rewrite rule, will still just show
www.site.com/index.php?pagename=events
How do I get this type of link, with a variable page_id_all to resolve to www.site.com/index.php?pagename=events&page_id_all=$
So far my research and experimentation hasn't returned any answers
Try using the QSA flag in your rule.
RewriteRule ^events index.php?pagename=events [QSA,L]

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