.htaccess rewrite rule for affiliate links - .htaccess

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.)

Related

Redirect the pages of a site with the question mark with the htaccess

I should redirect all links similar to this:
http://www.sitename.com/?918pae056d2bnnxpy
The problem is that these links do not have a variable that can be taken with htaccess ... so I was looking for a solution that could take all these URLs and redirect them to the home of the site.
Can someone help me?
It is unclear what you actually mean by "these links do not have a variable that can be taken with htaccess"... That question mark in the URL denotes request arguments ("GET arguments") that can very well be considered and modified on the level of the http server, so for example inside the apache http server's rewriting module.
Here is a simple example:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^/?$ /home.php?arg=%1 [END]
It internally rewrites every incoming request to / to /home.php if there is a query string present (so a GET argument specified). The argument itself will be handed over to the script as GET argument "arg".
There are other alternatives for most cases, but you did not tell us any details about your actual situation, so we cannot be more specific here.
The above is just an example, obviously you will need to adjust it to your needs. It is only meant as an explanation, to point you into the right direction.

how to rewrite a custom url in joomla using htaccess

I am designing a News Website using joomla 2.5
I want rewrite this url:
http://domain.com/categoryname/?format=feed&type=rss
to:
http://domain.com/rss/categoryname
Note: I'm using mode_rewrite .htaccess for joomla.
please help me quickly.
thanks to every body in this site.
Apache's mod_rewrite allows you to transform a url to a different url utilizing regex patterns.
The pattern applies to the path and allows you to do your in your example write a regex pattern like /rss/(.+) which will match anything beginning with /rss/ and has at least one character after. The parenthesis are called a capturing group and you can reference that in the second parameter in the RewriteRule directive.
The second part /$1/?format=feed&type=rss, references the first captured group in the pattern and places it in the new url.
Finally you want to signify that it is the last rule to be processed with an [L] flag.
This gives you a rule of:
RewriteEngine On
RewriteRule /rss/(.+) /$1/?format=feed&type=rss [L]
If you intend to pass query strings to this new url, you will need to add an additional flag QSA which will result in [L,QSA] in place of [L].

How to redirect only when there is something after .html?

I have found that there are some people with bad syntax links to our articles.
For example, we have an article with URL
http://www.oursite.com/demo/article-179.html
The issue is that lot of people have linked back to this article with bad syntax such as
http://www.oursite.com/demo/article-179.html%5Cohttp:/www.oursite.com/demo/glossary.php
Now, I added the following ReWrite Rule in the .htaccess file to take care of such links.
RewriteRule article-179\.html(.*)$ "http\:\/\/www\.oursite\.com\/demo\/article-179\.html [301,L]
But this has resulted in a Redirect Loop message. How can we fix this issue via htaccess rewrite rule. Basically, we need something in our rewrite rule that works only when there is one or more characters after the .html. If not, then it should not redirect.
Any help would be highly appreciated!
With best regards!
Use + instead of *. * matches zero or more, which causes the pattern to match for the redirected path too, + instead matches one or more.
Also you should make the pattern as precise as possible, ie don't just check whether it ends with article-179.html, better check for the full path. And if this all happens on the same domain, then there's no need to use the absolute URL for the redirect.
There's also no need for escaping the substitution parameter like you did, it's treated as a simple string except for:
back-references ($N) to the RewriteRule pattern
back-references (%N) to the last matched RewriteCond pattern
server-variables as in rule condition test-strings (%{VARNAME})
mapping-function calls (${mapname:key|default})
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
Long story short, theoretically this should do it:
RewriteRule ^demo/article-179\.html(.+)$ /demo/article-179.html [R=301,L]
or this if you really need the absolute URL:
RewriteRule ^demo/article-179\.html(.+)$ http://www.oursite.com/demo/article-179.html [R=301,L]

URL Rewriting based on form input

I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:
site.com/go/?id=whateverIType
and I want to change it to
site.com/go/whateverIType
I was thinking on Mod Rewrite, but if the user put something in the URL, like:
site.com/go/?dontwant=this&id=whateverIType&somemore=trash
I want to ignore the other variables but "id", and rewrite the rule.
What's the better way of get this done? Thanks in advance!
PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".
I'm not familiar with CodeIgniter, but you can try the following RewriteRule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.
Hope this helps.
Mod_rewrite supports conditions and rules with RegEx, so you could have a rule that matched the ?id=XXXX, that would extract it from the URL (keeping the other parameters), and rewrote the URL accordingly.
However... I don't think you want to do this, because if you rewrite the URL to be /go/Some+Search+Query, you won't be able to pick it up with say, PHP, without parsing the URL out manually.
It's really tough to have custom, SEO-friendly URLs with user input, but it is technically possible. You're better off leaving in the ?id=XXX part, and instead, using mod_rewrite in the opposite approach... take all URLs that match the pattern /go/My+Search+Terms and translate that back into something like ?id=My+Search+Terms, that way you'll be able to easily parse out the value using the URL's GET parameters. This isn't an uncommon practice - Google actually still uses URL parameters for user input (example URL: http://www.google.com/search?q=test).
Just keep in mind that mod_rewrite rewrites the URL before anything else (even PHP), so anything you do to the URL you need to handle. Think of mod_rewrite as a regular expression-based, global "Find and Replace" for URLs, every time a page is called on the server. For example, if you remove the query string, you need to make sure your website/application/whatever accounts for that.
In application/config/routes.php
$route['go/(:any)'] = "go/index/$1";
Where go is your controller and index is the index action.
http://codeigniter.com/user_guide/general/routing.html
You can use something like this in your .htaccess if you aren't already:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

htaccess redirect append query string?

I have an htaccess redirect that needs to forward the query string to the new URL, but it's getting dropped after the redirect. Can someone tell me what's wrong?
RewriteRule ^services/agents.*$ https://services.example.com/agents/ [R=301,L,QSA]
The same rule is working fine on my server. The problem should be something else.
I added the same rule on my server and I get the following redirect
http://mysite.com/services/agents/foo?foo=bar => https://services.mysite.com/agents/?foo=bar
Please note that you don't need to add the QSA flag since the target doesn't include any query string.
This article might contain some useful information to help you dealing with Htaccess and Query String.
In general there is no need to explicitly append the query or use the QSA flag if you don’t specify a query for the substitution. But as you said your rule doesn’t work, try this:
RewriteRule ^services/agents.*$ https://services.example.com/agents/?%{QUERY_STRING} [R=301,L]

Resources