.htacces rewrite rule with pagination - wordpress

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]

Related

Remove pagingation_base for specific taxonomy types

I want to remove the default pagination_base 'page' from the pagination URL for two taxonomy terms only. I don't want to remove the pagination base globally (like $wp_rewrite->pagination_base = '';).
I've used the below htaccess rules for testing purpose:
RewriteRule ^bransch/ljudisolering/page/([0-9]+)/?$ /bransch/ljudisolering/$1 [L,R=301]
RewriteRule ^bransch/ljudisolering/([0-9]+)/?$ /?index.php?bygg_branch=ljudisolering&paged=$1 [L]
When I test this htaccess rule checker like https://htaccess.madewithlove.be/ with URL http://site.local/bransch/ljudisolering/page/2, it shows the correct output.
But this doesn't change anything in WordPress site. Any ideas? Any help would be appreciated.

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

Redirecting query strings: mod_rewrite giving 404 error but tester showing (almost) correct

I'm trying to do a query string redirect to a more friendly url. This is within WordPress but I'm trying to do the mod_rewrite in the htaccess (am adding an extra rule, not touching any of the WordPress rules)
This is my desired url: localhost/site/events/term-1/term-2/term-3
And this is the actual url: localhost/site/events/?tax_1=term-1&tax_2=term-2&tax_3=term-3
Here is the code I have used:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^events/([^/]*)/([^/]*)/([^/]*)$ events/?tax_1=$1&tax_2=$2&tax_3=$3 [QSA,L]
</IfModule>
... WordPress rules here ...
This is giving me a 404 error when I go here: localhost/site/events/term-1/term-2/term-3 (but I have checked and localhost/site/events/?tax_1=term-1&tax_2=term-2&tax_3=term-3 is going to the correct place). I'm not getting any errors in the apache error log.
When I tested the rule on http://htaccess.mwl.be/ I was getting this result from the above input:
http://localhost/site/event/%3Ftax_1=term-1%26tax_2=term-2%26tax_3=term-3
which seems like it may be an encoding issue, but all the research I have done into the encoding of these inputs and outputs, this seems unlikely, and I'm wondering if this may be an issue with the tester.
Can anyone point out where I may be going wrong? I've been scouring the docs for mod_rewrites and I feel like my rules are written correctly. Is it an apache setting maybe? Or a conflict with WordPress?
Thanks
EDIT
Adding a NE flag to the rule is now giving me the correct result in the tester, but I am still getting the 404 result on the actual server.
I haven't technically solved my problem, but by using WordPress rewrite rules instead of mod_rewrite I have got the desired features.
For anyone else looking this what I added to my functions file:
function custom_rewrite_rule() {
add_rewrite_rule('^events/([^/]*)/([^/]*)/([^/]*)/?','index.php?page_id=PAGE_ID_OF_EVENTS_PAGE&tax_1=$matches[1]&tax_2=$matches[2]&tax_3=$matches[3]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_tag() {
add_rewrite_tag('%tax_1%', '([^&]+)');
add_rewrite_tag('%tax_2%', '([^&]+)');
add_rewrite_tag('%tax_3%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
Instead of hooking into the permalink events/, it needs to be sent to the index.php with the specified post id of where the page is, as the other custom rewrites will not run first, so WordPress won't know what events/ means.
The first function adds the rewrite rule and hooks it into the initialisation. The third parameter specifies when the rule will be run in the list of rules running - it has to be top as if it is bottom it will not run.
The second one adds the query vars as tags so that WordPress is aware of the custom query variables.

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