how to redirect URL containing query string and random numbers in .htaccess - .htaccess

When customers cancel a transaction on my site, they get redirected to the WooCommerce cart page with a query string containing randomly generated numbers at the end.
Example
https://www.example.com/cart/?woo-paypal-cancel=true&token=EC-5474842406066680S
(I need this redirect due to a plugin conflict between WP Rocket cache with CDN activated and WooCommerce. Long story.)
I'm wondering what exactly I would put in my .htaccess file to get it to redirect to
https://www.example.com/cart/
I've tried a number of variations I found on multiple pages here on Stackpath, but it wasn't redirecting. Obviously I'm missing something so I'm turning to the gurus.
Would be very grateful for your help.

To redirect /cart/?woo-paypal-cancel=true&token=<anything> to /cart/ you can try something like the following near the top of your .htaccess file (using mod_rewrite):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^woo-paypal-cancel=true&token=
RewriteRule ^cart/$ /cart/? [R,L]
The ? on the end of the RewriteRule substitution strips the query string from the request.
This is a temporary (302) redirect.

Related

Remove subfolder using htaccess

I know that this has been requested a million times but i haven't found an answer.
Basically I want to remove amp from my site but I've a million pages indexed using /amp so if I remove it this will cause a mess.
What I want to do is to redirect all the url like these
https://*/amp
will go to the same url without amp.
For example an article with this url
https://www.tuttosullapostaelettronica.it/blog/come-e-fatta-una-mail/amp
will be redirected to the same article without /amp like
https://www.tuttosullapostaelettronica.it/blog/come-e-fatta-una-mail
This has to be valid for whatever article url that end in /amp that has to be the /amp removed
Thanks!
Assuming you need to remove amp on requests to the document (first example) and /amp on requests to other URLs then you would need a rule like the following using mod_rewrite, near the top of the .htaccess file:
RewriteEngine On
RewriteRule ^(?:(.+)/)?amp$ /$1 [R=301,L]
The regex ^(?:(.+)/)?amp$ matches both amp and <something>/amp and removes amp or /amp respectively.
Test first with a 302 (temporary) redirect to avoid potential caching issues.

htaccess redirect if the url does not contain a specific character

I'm moving the site to a subdomain and need certain tag strings to go to the subdomain and some to remain on the main site. Problem is both have a similar tag system.
I need this type of request
https://www.site.co.uk/tags/example-tag
to go here:
https://sub.site.co.uk/tags/example-tag
but this type of request
https://www.site.co.uk/tags/view?tags=14-some-varriable
to remain unchanged and parsed to content without redirecting.
What would be the most recommended and best solution?
I have written some code to work around other redirects but this one is causing me a headache.
Cheers
For your this mentioned example, below should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/(.+)
RewriteRule ^ https://sub.site.co.uk/%1/%2 [R]

Rewrite rule in htaccess to keep parameters

I've written the following rewrite rule which works fine when no parameters (no page numbers, no products per page, no sort order etc)
RewriteRule ^(?!bench/).*cat_2.html(\.[a-z]{3,4})?(.*) "http\:\/\/www\.mysite\.co\.uk\/bench\/cat_2\.html\?mode\=allBrands" [R=301,L]
This makes sure the URL is optimized on Googles results. So
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands#
gets changed to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
mode=allBrands will always be set.
So if i click on a link to go to
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands&ppp=64&sort=desc&page=2
it gets redirected to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
which is the first page.
Any help would be great.
change [R=301,L] to [R=301,L,QSA]
More info : https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
You need to add a QSA flag to your rewrite rule, so that the brackets look like this:
[R=301,L,QSA]
This tells apache to append any existing query string to the new query string in the target (mode=allBrands).

How to keep website available to some users oonly using htaccess

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.

Redirecting an already redirected page

RewriteRule ^teamstore/(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
RewriteRule ^teamstore-(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
Here's the situation. We've already had theese urls rewritten, but we want to change the formatting of these pages so they're separated by hyphen instead of a slash.
I tried redirectmatch but this added additional php value parameters to the end of the urls. It came out to be
RedirectMatch 301 /teamstore/(.*) http://www.domainname.com/teamstore-$1/
Here was the result...
teamstore-valuehere//?teamproduct=2352323&productes=true
I want someone who types in the original address teamstore/info/ to get directed to teamstore-info/ - Any ideas on how to accomplish this?
The main reason is to avoid duplicate content issues with existing links in Google Search Results.
Order of the rules can matter, but if you want to 301 redirect the initial request for SEO reasons, then I would change your RewiteRules to the following.
RewriteRule ^teamstore/(.*)/$ teamstore-$1/ [R=301,L]
RewriteRule ^teamstore-(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
This will first translate teamstore/info/ to teamstore-info/ and send the appropriate 301 response. Upon second pass it will redirect to the php you want.
Note, this is not ideal as far as performance. Yet, it does accomplish the goal of making Google happy.

Resources