Alter GET variable in Wordpress - 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]

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

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 remove the the word "category" from the URL of a WordPress category NOT the post?

I have a category by the name "mycategory".
The URL of the category is currently - http://mixerp.org/blog/blog/category/mycategory/
but I need to change that URL to - http://mixerp.org/blog/blog/mycategory/
I tried playing around with the permalink settings but unfortunately I was unable to solve my problem. Currently, I have set the permalink settings to "Day and name". Can anyone kindly help me on this ?
You should use a Custom Permalink if the other options aren't working.
Make sure you delete the %category% part of the permalink, and this should solve your issue.
you can do this by change in .htaccess file,
First backup your .htaccess file. Then, open it and append the following line:
RewriteRule ^category/(.+)$ http://mixerp.org/blog/blog/$1 [R=301,L]
Once saved, your categories pages will be displayed like this:
http://mixerp.org/blog/blog/mycategory/
But i will prefer the answer given by Ronan Murphy, which is easy and safe

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