htaccess redirection matching urls to grandparent directory - .htaccess

A search engine keeps hitting my site regularly for non-existing pages that it randomly generates. I want to put an end to this silliness using a 301 rule in my htaccess file.
The most usual address is
site.tld/blog/welcome-back/x/xx/
where x is a random number but sometimes I get hits on
site.tld/blog/welcome-back/xx/xx/
or text instead of numbers.
I want to redirect all these hits to the real existing page
/blog/welcome-back/
Thanks in advance for any help!

This should do what you need:
RewriteEngine On
RewriteRule ^blog/welcome-back/.+$ http://site.tld/blog/welcome-back/ [R=301,L]

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.

how to redirect URL containing query string and random numbers in .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.

Is there a 301 wildcard match?

I am having great difficulty fielding all the 301 redirects that seem to be needed following a complete site redesign. Entire sub-directories and their extensive content no longer exist. If, for example, 'myolddata' was a folder that no longer exists, and was full of countless files that each no longer exists either, I would like to say "forget all that and just go to the index page". There seems no other way of closing the door on Google & Bing endlessly reporting squillions of 404's to me. Is there a way of saying in effect:
Redirect 301 /myolddata/ /index.html
Redirect 301 /myolddata/* /index.html
where the first says 'forget the folder' and the second says 'and forget everything tht was in it'?
The second part to this issue is that old PHP files and their arbitrary search parameters are logged too. Stuff like:
oldfile.php?this=1&that=2&somethingelse=3
oldfile.php?this=Tom&that=Dick&somethingelse=Harry
You get the picture. Millions of them. How can I set up a 301 to say "forget oldfile.php and any parameter with it imaginable - they are all gone!"
Your assistance, comments and advice would be incredibly valuable, so all insights welcome please!!
Better use mod_rewrite rules for this using 301 status code. Use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(oldfile\.php$|myolddata(/|$)) /index.html? [L,NC,R=301]
? in the end will strip any existing query string.

htaccess redirecting from rewritten dynamic urls to new dynamic urls

i am experiencing a very unique problem and i hope someone can help!
so we have recently created a new ecommerce website and we made it live and everything was working great but when we to implement our 301's from our old pages we were getting some wierd things
so the code below actually works
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
this code does not
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
the output when i try to do this redirection is "Invalid parameters specified!" on a blank webpage and in the address bar it has this
http://mysite.com/index.php?cat=1/sub_directory/
we were thinking that maybe the problem is because our old pages were dynamic but mod_rewrite was used to create more readable urls and we have also deleted all our old files because they were interfering with our new pages rendering
any help would be greatly appreciated!
thanks
That is strange, as redirect should only match the specific url listed, where as it looks like its behaving like rewriterule and partially matching the subdirectory url against the first rule..
try putting the more specific rule above the less specific, like so:
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
That way the more specific rule will be hit first, and the /directory/ only rule will only match if more specific matches above fail
alternatively, you could try RewriteRules:
RewriteRule ^directory/$ http://mysite.com/index.php?cat=1 [R=301,NC,L]
RewriteRule ^directory/sub_directory/$ http://mysite.com/index.php?cat=2 [R=301,NC,L]
the ^ and $ anchors should prevent any unwanted partial matching

.htaccess rewriting certain word in url

I cannot seem to find the answer to this
I am looking for a .htaccess rewrite way to turn this url
http://www.mydomain.com/Virtuemart.html?keyword=adobe&page=shop.browse
to
http://www.mydomain.com/virtuemart.html?keyword=adobe&page=shop.browse
So basically i just want to change the capital V
There are many pages and variables after the ? so doing it one by one is almost impossible
is their an easy way to do this.
Many thanks for your time
David
Using mod_rewrite (under apache), you can stick something like this in the .htaccess file in your document root:
RewriteEngine On
RewriteRule ^Virtuemart.html /virtuemart.html [L,QSA]
The query string just gets appended to the end untouched. If you want to redirect the browser, add a R=301 flag to the end (or just R if you don't want 301). This makes it so when someone goes to http://www.mydomain.com/Virtuemart.html, they'd actually get served the page at /virtuemart.html.

Resources