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.
Related
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]
I am on Wordpress and right now using Yoast Seo Pro which has a redirection section including a Regular Expressions redirects section.
How do I redirect say /mycategory1/page/pagenumberhere/ to
/category/mycategory1/page/pagenumberhere/ ?
So I only need to make one redirect that handles all possible page numbers?
I have tried /mycategory1/page/([0-9]) to /category/mycategory1/page/$1
It looks like it redirects to /category/mycategory1/page/pagenumberhere/ but there is an err_too_many_redirects on the /category/mycategory1/page/pagenumberhere/ with this rule added so I have removed it again.
If you can help with code into the .htaccess instead perhaps I could try that.
Your rule already looks promising.
However, as #starkeen pointed out, the regular expression mycategory1/page/([0-9]) matches every request containing mycategory1/page/ with some trailing number. This is true of category/mycategory1/page/ as well, as it also contains "mycategory1/page".
If you want to match requests starting with mycategory1/page, you must anchor the regular expression at the beginning with ^, see Apache mod_rewrite Introduction - Regex vocabulary
RewriteRule ^mycategory1/page/([0-9]) /category/mycategory1/page/$1 [L]
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.
I am attempting to redirect all requests of the main stylesheet called styles.css to the minify script of min/g=css so that I may be able to work on the CSS file live and just have the server use redirect to the minify script.
Basically, this in my html file...
<link rel="stylesheet" href="css/styles.css" type="text/css">
turns into this
<link rel="stylesheet" href="min/g=css" type="text/css">
when the server requests it.
Here is my attempt so far, but it doesn't seem to be working at all. The regular css file just loads on the server..
RewriteRule ^css/styles\.css$ min/g=css
Along these same lines, I also have a fear about when I start building subdirectory pages. Does this rewrite rule need to be an "absolute path"? Thanks for any help everyone!
Along these same lines, I also have a fear about when I start building subdirectory pages. Does this rewrite rule need to be an "absolute path"? Thanks for any help everyone!
The rewrite engine strips off the leading slash of the URI when applying rules in an htaccess file, but if the rules are in server/vhost config, then you need the leading slash. The targets (the min/g=css part of your rule) also doesn't need to be an absolute path, but if there is a leading slash, apache will apply that to the base (usually the document root). Without a leading slash, apache tries to guess whether you mean a file-path or a URI-path, sometimes it guesses wrong. If you include a RewriteBase directive, then apache will always use that as its base.
So the question here really becomes: where do you have these rules? In an htaccess file, you're only left with the question of the base. If it's in server/vhost config, you need to know the base as well as add a leading slash to the regex: ^/css/styles\.css$. Or you can be safe and make it optionsl: ^/?css/styles\.css$
Now, the base. The base is where relative paths get appended to. Your css link is this css/styles.css. If that page is loaded from, say, this URL: http://domain.com/some/path/mypage.html, then the base is http://domain.com/some/path. and the resolution of the relative path is http://domain.com/some/path/css/styles.css. In this example, your rules need to go in the directory where /some/path points to and you need a:
RewriteBase /some/path/
above your rule. Otherwise, you can simply append that base to the regex and target of your rule:
RewriteRule ^/?some/path/css/style\.css$ /some/path/min/g=css [L]
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. :)