Wordpress htaccess rewriterule without changing URL - wordpress

In WordPress I'm using the Events Manager plugin, which uses the /events/subpage path in the URL.
I need to tweak things slightly so that /courses/subpage rewrites to /events/subpage i.e. the content of /events/subpage is shown when going to /courses/subpage. The URL should stay as /courses.
I thought this rule would do it:
RewriteRule ^courses/?(.*)$ /events/$1 [L]
But the URL changes from /courses to /events so it looks like my rule isn't quite right.
EDIT
Strangely I can put anything in place of /courses and it resolves to /events anyway. Confused!

Think I’ve cracked it.
I uninstalled the rewrite plugin and replaced it with Rewrite Rules Inspector. Then I added the following to functions.php
add_rewrite_rule(‘^course/([^/]*)/?’, ‘index.php?event=$matches[1]‘, ‘top’); // single event
In Rewrite Rules Inspector is said the rule was missing. I flushed the rules using the plugin and everything appears to be working now. It looks like the original rewrite plugin wasn’t flushing the rules properly.

Related

How can I redirect one missing image to the correct path, but only that specific image?

I have a wordpress + woocommerce site with one particular issue. We have an icon image (png) that appears in the product and from time to time it removes the absolute URL of the image to a relative one using ../ and it creates an invalid URL. I need to catch all calls to that specific image, no matter what URL it is, of course, only the 404 ones, and redirect it to the right path.
For example, the right image is:
https://colmena.co.il/wp-content/uploads/2021/01/event-icon-xxs.png
And this is one of the wrong calls:
https://colmena.co.il/shop/eventos/wellness/wp-content/uploads/2021/01/event-icon-xxs.png
I need the second one to redirect to the first one, but I cannot use the full wrong URL because it changes based on the categories and subcategories (the /shop/eventos/wellness part).
I tried a couple of catch all examples found here and elsewhere trying to adapt them, but I just made it worse so I removed them all.
Any help is appreciated!
It is questionable if what you attempt really is a good idea. I personally would always prefer to fix the actual cause of the issue instead of trying to handle the symptom, as you suggest...
That said I assume the following is what you are actually looking for:
RewriteEngine on
RewriteRule ^/?shop/(?:[^/]+)/(?:[^/]+)/wp-content/uploads/2021/01/event-icon-xxs\.png$ /wp-content/uploads/2021/01/event-icon-xxs.png [L]
I think however that you can simplify that, since you most likely do not use a similar path for other locations:
RewriteEngine on
RewriteRule /?event-icon-xxs\.png$ /wp-content/uploads/2021/01/event-icon-xxs.png [L]

.htaccess change directory image

I have this blog and I've looking around the way to change my image path but nothing really works for me.
This is the URL where my image will display:
http://localhost/blog/article1/
inside is located <img src="images/logo.jpg" />
but with this URL the server searches for:
localhost/blog/article1/images/logo.jpg
And the real URL for my image is: localhost/blog/images/logo.jpg
I've tried with a lot of options but so far none have worked for me.
Note: the blog is made in WordPress, but some articles come with code from another migration, so I probably cannot change the whole articles.
What I need is to elevate subdirectory to localhost/blog/images
Well, you might get away with redirecting all calls that end with /back/back1.gif to that directory, so it wouldn't matter where the call came from. That does mean you should not wish to call this to some other subedirectory, but I imagine you don't.
something like this (guessing here, cann't test, so read up on the rwriting there :)
RewriteRule ^(.*)/images/$(.*) http://test.example.com/back/$2 [L,R=301]
basically you are rewriting everything that has '/images/' in it to that static adress, pasting whatever was after images after the new asdress (the $2 thingy) and then indicating that this is the last command to parse (to stop strange things in the htaccess) and that you want a 301 (permanently moved) code to be sent.

Code needed for htaccess redirect in WordPress

I've been fixing a strange htaccess issue on my client's WP sites. I found a solution by putting the ReWrite code in the htaccess of each site and then adding a custom permalink code in permalinks and it seems to work EXCEPT for one section.
There are a total of 9 sites with this issue. All of them have a highlights plugin on the front page that's pulling story and images and displaying them on the home page and then a link that goes to the story full page. For my above permalink fix, out of the 9 sites, my fix works on 5 of them but on 4 of them in the highlights section, when you click a link I'm getting a 404.
The strange thing is, with my above fix, the highlight page still publishes, but not with my fix. For example, when you click the "fixed" link it should open as:
mysite.com/central/index.php/highlights/my-page
...but I get a 404. But all I have to do is delete the "index.php" in the above, and the page itself still works:
mysite.com/central/highlights/my-page
So what I now will do is write re-write code and put it in the root htaccess file so that when the page:
mysite.com/central/index.php/highlights/my-page
...is called it will re-write to:
mysite.com/central/highlights/my-page
...and that should fix this pesky issue. Here's the catch - I'm not good at writing code and I used my own hosting company's re-write generator to create a sample for me to see but I can't use it to generate what I need because it only generates for my domain. Since I don't know how to post code to this forum, below is a snap shot of it:
http://designerandpublisher.com/images/rewite-code.jpg
Is there a place where I can just copy and paste the URLs and it will generate the code so it looks like the above snap shot? Or is there some kind of tutorial of what / and \ and dollar signs and the periods and why they're there? I used to set up redirect code a lot simpler than this but it seems more complex now and i'm just hoping there's some kind of generator? Thanks for any assistance.
Adding the following rule should work:
RewriteRule ^central/index.php/(.*) /central/$1 [R=302]
it should turn mysite.com/central/index.php/highlights/my-page to mysite.com/central/highlights/my-page
(Change R=302 to R=301 when you are sure the redirect works)

Evaluating a external rewrite rule before internal wordpress rewrite rule

I am been plucking out my hairs since last few days trying to solve this problem:
I want to convert my urls from format
http://example.com/prodsearch/category/tag1-tag2-tag3-tag4
to
http://example.com/prodsearch/index.php?tag=tag1+tag2+tag3+tag4&cat=category
to start with I added following rules to my .htaccess
RewriteRule ^prodsearch/(.*)/(.*)-(.*)$ prodsearch/$1/$2+$3 [L]
RewriteCond %{REQUEST_URI} !^prodsearch/(.*?)/(.*)\-(.*)
RewriteRule ^prodsearch/(.*)/(.*)$ index.php?tag=$2&cat=$1 [R,QSA,L]
Here rule1 recursively replaces all '-' between tags by '+'
and rule2 does the actual rewrite once there are no more '-' left between tags (checked by RewriteCond)
These rules actually work, but the problem is they redirect (with url change) to new url pattern, I don't want an explicit redirection. On removing 'R' flag from last rule, the whole thing stops working and I start getting 404 page in my wordpress install.
Can some one explain why this is happening and how to do this without explicit redirection.
Also I tried including these rules into wordpress, hoping when called by wordpress rules may work without redirection. I used following add_rewrite_rule() calls:
add_rewrite_rule('prodsearch/(.*)/(.*)-(.*)$','price-list/$1/$2+$3','top');
add_rewrite_rule('prodsearch/(.*)/(.*)$','index.php?tag=$matches[2]&cat=$matches[1]','top');
Now wordpress detects my first rule as external rule and flushes it to .htaccess file, this screws my execution order, now first rule 2 gets evaluated first(being part of internal wordpress rewrite rule set) and rule 1 gets executed later, hence again 404 page.
Is it possible to tell wordpress not to consider my first rule as a external rule and not to flush it to .htaccess.
Or does anyone has any idea to make this kind of rewrite work? Thanks a lot.

Hide "www" text in URL bar (firefox)

I was wondering if it is possible to hide the "www" text in the URL bar (only in Firefox) using CSS in Stylish addon or/and Java in Greasemonkey.
I want this to make Firefox even more compact.
This is some CSS code that i found for URL bar in firefox that will modify the text size using Stylish. Hope it can help.
#namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
.urlbar-input-box,
.searchbar-textbox {
font-size: 11px !important;
}
Update
I don't want to remove the "www", I just want to hide it from the url bar.
www.example.com and example.com are two different things. Typically they are considered the same, but www. is in fact a subdomain.
Therefore, hiding it would be misleading to the user.
You have to use .htaccess for this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Replace example.com with your domain name.
I don't think this would be a good idea, because www. is actually a subdomain therefore removing it may cause problems with certain websites because sometimes they may need www. for some stuff. Also if they use other subdomains, they might want cookies (or something that uses the domain) to be set only on their www. and not *.example.com which is what will happen without www. (a subdomain) in front of it.
So no, I don't think it is possible to do this nor is it a good idea. Let the website owners/developers decide if they want www. in front or not. It's only an extra three or four characters — it won't do much harm to leave it. :)

Resources