I am working on a WordPress plugin that shows Gutenberg blocks depending on user categories. For instance: If a user has the category "winter", blocks with the category "winter" are shown and blocks with the category "summer" are NOT shown - vice versa.
I store the user clicks in a cookie ("user-data"). This cookie is set via JS and modified on every page load via ajax. A second cookie ("user-cat") contains the ID of the estimated user category.
I use WP Rocket for caching. Unfortunately WP Rocket stores the pages as static content and that's why the PHP rendering of the Gutenberg blocks isn't dynamic.
Because of that I would like to add the current user category as a parameter to the URL. If a user has the category "winter" with the ID 10, the URL should look like this: www.mydomain.com?x=10. That way WP Rocket creates different static versions of each page.
Following pages would be stored separately by WP Rocket:
www.mydomain.com
www.mydomain.com?x=10
www.mydomain.com?x=100
I tried to achieve this by modifying the .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(/wp-login.*|/wp-admin)
RewriteCond %{QUERY_STRING} !x [NC]
RewriteCond %{HTTP_COOKIE} user-cat=([^;]+) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1?x=%1 [NC,QSA,R,L]
This solution works fine on standard WordPress pages, but it seems hacky to me and does not work on multisite installations. I would like to use proper WordPress code and not edit the .htaccess manually.
Is there any other/better way to achieve this? Does anyone have a better idea how to create dynamic Gutenberg blocks when using caching plugins?
Related
I want to completely block requests for urls in this format:
/cat/pathfinder/pathfinder-accessories/?add-to-cart=27368
or
/cat/pathfinder/?add-to-cart=27368
I tried to do this via HTaccess :
RewriteRule ^(.*)cat$^(.*)?add-to-cart=$^(.*)$ - [F]
But that doesn't match. Can you see why not?
I have other, simpler rewriterules in the same .htaccess file that are working.
Context:
I have an online shop site being pounded by automated requests to 'add to cart'. The shop has thousands of products, so the impact of so many simultaneous requests on memory & processor is horrible. (The shop is Wordpress with Woocommerce, if that matters)
I changed things so that there are no longer pages of 20 products where the bot can hit 'add to cart' simultaneously on all of them any more, but they are still hitting them, so clearly they have a list. I've been unable to block the requests by IP, as they are coming via Cloudflare from many locations.
Query string is not matched using RewriteRule.
You can use this rule instead:
RewriteCond %{THE_REQUEST} /cat/[^?]*\?add-to-cart=\d+ [NC]
RewriteRule ^ - [F]
My rule in the htaccess file
RewriteRule ^brands/([^/.]+)/?$ browse-brand.php?brand=$1 [L,QSA]
Turns the midly ugly
www.mysite.com/brands.php?brand=$brandName
into a more attractive
www.mysite.com/brands/brandName/
Each brand page is used to display hundreds of products relating to that brand so is split in to pages and can be filtered to show 20/50/100 products or order by price/bestsellers etc
So my newly made 'pretty urls' become ugly again when attaching the sorting and ordering variables using QSA, such as:
www.mysite.com/brands/brandName/product_show=20&product_sort=sortpricedesc&page=2
Obviously the pages won't perform without these extra variables. I suppose it's possible to change my RewriteRule to include these, like so:
www.mysite.com/brands/brandName/20/sortpricedesc/2/
but that's still a bit ugly.
Is there a way to hide these variables from displaying in the URL bar? How do you deal with product catalogues with multiple pages?
You can use this .htaccess:
RewriteRule ^brands/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ browse-brand.php?brand=$1&product_show=$2&product_sort=$3&page=$4 [L,QSA]
RewriteRule ^brands/([^/.]+)/?$ browse-brand.php?brand=$1 [L,QSA]
The final page is the same browse-brand.phpwith new variables. But you also can change the name in this case.
For the past 4 weeks (yes you read right, 4 weeks) I 've been trying to redirect my website to a new domain.
Here's what I want to do:
Redirect the ROOT of my website from ukmotorhomehirerental to leisurerentalsdirect.com
Without redirecting all the subfolders. ie. if I click on a link in google to one of our minor pages, I want to land on that page on the new domain without being redirected to the index page. It worth mentioning that I do want to also redirect the minor pages on a page to page basis.
I'm doing it like this (this works perfect)...
RedirectRule ^/pages/contentPage.asp\?QN=94 http://www.leisurerentalsdirect.com/pages/contentPage.asp?QN=94 [I,R=301]
However this This is were the problem lies...
RedirectRule ^/ http://www.leisurerentalsdirect.com [I,R=301]
I know the above implies to the browser that all traffic requesting ukmotorhomehirerental will be forwarded to leisurerentalsdirect
I think I need some sought of condition applying, but I just cannot understand how to do this, can anybody help?
You don't actually say what the problem is, but I think you want www.ukmotorhomehirerental.com to go www.leisurerentalsdirect.com and www.ukmotorhomehirerental.com/somepage to go to www.leisurerentalsdirect.com/somepage, right?
If so, you should only need a single redirect rule:
RewriteCond %{HTTP_HOST} ^www\.ukmotorhomehirerental\.com/? [NC]
RewriteRule ^(.*)$ http://www.leisurerentalsdirect.com/$1 [L,R=301]
This will simply redirect any page on ukmotorhomhirerental.com to the same page (or root, if none) on leisurerentalsdirect.com.
my website is under construction and i would like to show the website to my friends ,And deny to all users. i used particular ips in htaccess files,but ip is not static . Is there any way to keep mac address ? or any way
You can't match against a MAC Address using directives in an htaccess file. You could either
Put a password on your actual content and everyone without the password gets forwarded to an "Under Construction" page (possibly using ErrorDocument 403 /under_construction.html`)
Look for a special query string that you can give out to only your friends:
RewriteEngine On
RewriteCond %{QUERY_STRING} !special_var=my_buddies
RewriteRule ^ /under_construction.html [R=301,L]
So here, any url without the special_var=my_buddies query string ( http://hostname.com/some/content.php?special_var=my_buddies ) gets redirected to the under construction page
You can do something similar by setting a cookie. Create a small landing page called something like /my_buddies.php that simply does a set_cookie() call to create a my_buddies cookie (doesn't have to be php, and the cookie can be called anything). Then do something similar as the query string:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !my_buddies
RewriteRule ^ /under_construction.html [R=301,L]
Essentially the same thing. If there's no my_buddies in the cookie string, redirect to the under construction page.
There's probably other ways to get do something like this, but can't think of anything off the top of my head.
I have a slight problem with .htaccess redirect.
I have a dynamic site with 2 levels of variables - content="type -(alpha)" and ID="number" but this is very not seo friendly what I really would like to create is a rewrite rule that generates a "friendly" url for serach engines & users alike. Very much like WordPress does.
Each ID is already unique (obviously) and on creation is creating a unique "permalink" field so for example ID=1 has a "permalink field" as "2009/10/27/page title" and ID=100 would be "2010/10/27 page title".
I would like folder/wall.php?content=type&ID=number to redirect to folder/permalink.php/html/htm (don't mind a non dynamic extension)
Any clues? - this is not right (I know) but it also "breaks" my css file
RewriteEngine On
RewriteRule wall/content/(.*)/ID/(.*)/ wall.php?content=$1&ID=$2
RewriteRule wall/content/(.*)/ID/(.*) wall.php?content=$1&ID=$2
You need to make sure you have a URL convention that won't conflict with other files on the server. For example, if your page is displaying "Products" and you don't have an actual folder in the root of your server called /products/, you could do this
RewriteEngine On
RewriteRule ^products/([^/\.]+)/([^/\.]+)/?$ wall.php?content=$1&id=$2 [L,NC,QSA]
Replace "products" in this example with whatever name is most appropriate for what your wall.php page does.
This would equate to the following URL
www.yoursite.com/products/title/29/
Which would rewrite to
www.yoursite.com/wall.php?content=title&id=29