.htaccess 301 redirect of a dynamic url with defined first part and random last part - .htaccess

I'm new to this, so i searched and could also do alot alone. But now there's a point I need your help. It's about a e-commerce page.
I want to redirect the old URLs from our brand pages to the new one.
The old brand URLs looks like:
http://www.domain.com/index.php?shop_q=empty&cid=0&man=32&pf=0.00&pt=10000.00&p=shop&action=showproducts&list=date_asc&limit=10
the only thing i need out of this long url is man=32, what stands for the manufacturer (brand) id.
the new URL looks like:
http://domain.com/brand/8_brand-name
So I want that the rule to redirect every url with man=32 in it to the new brand URL, even if there are more characters in the URL.
Thanks for your help!

Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)man=32($|&)
RewriteRule ^ /brand/8_brand-name? [L,R=301]
The condition's regex pattern, (^|&)man=32($|&), matches a specific query string parameter, and will match it no matter where in the query string it is.

Related

.htaccess rewrite rule for affiliate links

I've many links of this type:
https://example.com/?redirectTo=G04BIQ8LGG&redirect_prodid=xyz-G04BIQ8LGG
I need to redirect to amazon affiliate link:
https://www.amazon.com/dp/G04BIQ8LGG?tag=mytag-21&linkCode=ogi&th=1&psc=1
The only part to take from old url is the product code (ex.G04BIQ8LGG)
Someone can help me with .htaccess rule and regex?
Thanks!
unfortunately no, I'm not very good with regex.
The regex is very similar as in the linked question. However, the required mod_rewrite directives themselves are much simpler in this case.
For example:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^redirectTo=([^&]+)
RewriteRule ^$ https://www.amazon.com/dp/%1?tag=mytag-21&linkCode=ogi&th=1&psc=1 [NE,R=302,L]
I've anchored the redirectTo URL parameter to the start of the query string, since that is how it appears in your example. In the linked question, the URL parameter can appear anywhere in the query string since that would seem to have been a requirement in that question.
Since the URL parameter value is used in the URL-path of the redirected URL, the NE (noescape) flag is required to prevent a %-encoded URL param value being doubly encoded in the resulting redirect. (Although this is not an issue if this URL param value is never %-encoded - it doesn't necessarily look as if it would be.)

htaccess How to redirect old dynamic url to the new one

My web uses links which are dynamic set by code in htaccess (bellow):
RewriteRule ^(.*),(.*),([a-z0-9-_.]+),([a-z0-9-_.]+),([a-z0-9-_.]+)$ $4.php?n=$1&z=$2&t=$3&v=$5 [L,NC,NS,NE]
In effect links looks like this (example):
www.mypage.com/$1,$2,$3,$4,$5
I want to redirect dynamic links in htaccess from old to new one which will have a structure like this (without $5 parameter):
www.mypage.com/$4/$1-$2/$3
Redirection is necessarily especially for redirect old links availble in search engines to new one.
Thanks for a help.
I don't have an Apache instance to hand to test against just now but off the top of my head something like this should work:
RewriteRule ^(.*),(.*),([a-z0-9-_.]+),([a-z0-9-_.]+),([a-z0-9-_.]+)$ $4/$1-$2/$3 [L,R=301]
RewriteRule ^([a-z0-9-_.]+)/(.*)-(.*)/([a-z0-9-_.]+)$ $1.php?n=$2&z=$3&t=$4 [L,NC,NS,NE]
The first rewrite redirects your old URLs (1,2,3,4,5) to the new ones (4/1-2/3) using a 301 to tell search engines to drop the old URLs in favour of the new ones.
The second rewrite takes the new format and maps it to your actual script.
Note how the 5th param is dropped when transforming old to new.

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.

How to rewrite long URL with .htaccess?

htaccess file with the following:
RewriteRule ^home?$ index.php
This works great for rewriting the url of my home page. Users are able to post articles on my site so the url for all the various pages are as follow:
http://example.com/article.php?id=22
Let's pretend there are 1'000 articles how can I rewrite each article to something like this:
example.com/articleId/articleTitle
For example:
example.com/56732/How-to-bake-bread
Is it possible to extract the data of an article and use it to rewrite the article's URL?
I guess you're looking for that:
RewriteRule ^([0-9]+)/(.+)?$ article.php?id=$1 [NC,L]
The first part of the regex ([0-9]+) captures the id in the url and is passed in the value passed in $1, if you wanted to use as well the (.+) part you could get it using the value in $2.

htaccess 301 from old site to new site

I have a client domain with thousands of pages that are moving to a new domain. The naming convention of the .html has changed, and I know htaccess can handle this somehow.
Here's an example:
old site:
http://oldsite.com/state/cityname-index.html
new site:
http://newsite.com/state/computer-support-cityname-index.html
This is beyond my understanding at the moment. I'd appreciate a little help. Thanks!
If you want to redirect the old website pages to the new one, you can use rewriting like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com$ [NC]
RewriteRule ^([^/]+)/(.+)-index\.html$ http://newsite.com/$1/computer-support-$2-index.html [QSA,R=301]
The first line activates the rewriting in the htaccess.
the second one checks if the current request is for the old website.
if yes, the third line is activated, wich contains a regular expression and a new link to redirect to if that regular expression if matched, the [QSA,R=301] at the end are flags that will affect the rewrite rule.
This rule I wrote to you captures the state (first parentheses) and the cityname (second parentheses) and then it redirects to the new website replacing the $n (where n is the number of the parenthese from before) with the captured content.
The QSA flag (Query String Append) will add any parameter from the old site request to the new generated request.
The R=301 flag will generate a browser Redirection with code 301 (permanant redirect).
For more information about mod_rewrite see http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Resources