Redirect the pages of a site with the question mark with the htaccess - .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.

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 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]

htaccess redirect not working for long url

How do I redirect the following long link:
http://www.vbpmonitor.com/index.php?option=com_content&view=article&id=24&utm_source=MagnetMail&utm_medium=email&utm_term=asmith#panaceainc.com&utm_content=EVVWP040716&utm_campaign=White%20Paper%3A%20Optimizing%20VBM%20Quality%20Tiering%20for%20Physicians
to
http://www.vbpmonitor.com/optimizing-vbm-quality-tiering-for-physicians
Redirect 301 /index.php?option=com_content&view=article&id=24&utm_source=MagnetMail&utm_medium=email&utm_term=asmith#panaceainc.com&utm_content=EVVWP040716&utm_campaign=White%20Paper%3A%20Optimizing%20VBM%20Quality%20Tiering%20for%20Physicians http://www.vbpmonitor.com/optimizing-vbm-quality-tiering-for-physicians
As said above in the comments I suspect that you have a glitch in your logic here and that in reality you want to redirection to work the other way 'round. Redirecting from the long to the search engine friendly URL simply does not make any sense. So:
Using a Redirect rule you could try that instead:
Redirect 301 /optimizing-vbm-quality-tiering-for-physicians /index.php?option=com_content&view=article&id=24&utm_source=MagnetMail&utm_medium=email&utm_term=asmith#panaceainc.com&utm_content=EVVWP040716&utm_campaign=White%20Paper%3A%20Optimizing%20VBM%20Quality%20Tiering%20for%20Physicians
This will redirect an incoming request to the short URL to the actually existing long URL. That is the usual scenario.
If however you really want to redirect that short URL to the long version, then you cannot do that with a Redirect rule. This might for example be the case if you accidentally sent out that long URL and have a working redirection setup for the short version. Unfortunately you do not explain anything about that in your question or comments, so I can only guess here.
You'd have to use the more flexible rewriting module and use a combination of RewriteCond and RewriteRule. That allows to "cut out" specific patterns of request URLs and to "redesign" how the request should look like after the rewriting.
This would be a simple example that applies two conditions to rewriting the request for file index.php to the long URL:
RewriteEngine on
RewriteCond %{QUERY_STRING} view=article
RewriteCond %{QUERY_STRING} id=24
RewriteRule ^/?index\.php$ /optimizing-vbm-quality-tiering-for-physicians [L,R=301]
Note: this version should work both in the http servers host configuration and also in those .htaccess style files. Where you always should prefer the first option if you have access.
As said above, I can only guess here with the sparse information you provided. I picked two out of many request arguments, since those appear to be the ones best suited as distinct identifiers. But you may have to tweak things. Note that per default RewriteConds are combined by a logical AND, so they both have to resolve to something truish.
For more precise details about this stuff I would like to point you to the official documentation of those modules again. The documentation is extremely precise, well written and comes with good examples. I would always prefer the information there to snippets you find somewhere in the internet or partial answers to questions...
http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

.htaccess rewrite to add query parameter

I need to modify all requests bearing the form
http://example.com/dw2/dokuwiki/doku.php/page to
http://example.com/dw2/dokuwiki/doku.php/page?do=export_xhtml
The page bit is variable - it corresponds to each paage in the wiki. I should mention that given the way dokuwiki syntax works page could contain one or more colons. e.g. glossary:archive.
The intent here is to extract the bare page content (shorn of the header, sidebar etc) of the wiki for distribution via a CDN. This does not give a complete solution since dokuwiki still leaves in a lot of unrequired verbiage in the exported markup file but gets me most of the way there. I'd much appreciate any help with this.
Place this rule as your very first rule in /dw2/dokuwiki/.htaccess:
RewriteEngine On
RewriteBase /dw2/dokuwiki/
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(doku\.php/[^/]+)/?$ $1?do=export_xhtml [L,NC,QSA,R,NE]

Why does this cause an infinite request loop?

Earlier today, I was helping someone with an .htaccess use case, and came up with a solution that works but can't quite figure it out myself!
He wanted to be able to:
Browse to index.php?id=3&cat=5
See the location bar read index/3/5/
Have the content served from index.php?id=3&cat=5
The last two steps are fairly typical (usually from the user entering index/3/5 in the first place), but the first step was required because he still had some old-format links in his site and, for whatever reason, couldn't change them. So he needed to support both URL formats, and have the user always end up seeing the prettified one.
After much to-ing and fro-ing, we came up with the following .htaccess file:
RewriteEngine on
# Prevents browser looping, which does seem
# to occur in some specific scenarios. Can't
# explain the mechanics of this problem in
# detail, but there we go.
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
# Hard-rewrite ("[R]") to "friendly" URL.
# Needs RewriteCond to match original querystring.
# Uses "?" in target to remove original querystring,
# and "%n" backrefs to move its components.
# Target must be a full path as it's a hard-rewrite.
RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$
RewriteRule ^index\.php$ http://example.com/index/%1/%2/? [L,R]
# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
RewriteRule ^index/(\d+)/(\d+)/$ /index.php?id=$1&cat=$2
Whilst it might seem to be a somewhat strange use case ("why not just use the proper links in the first place?", you might ask), just go with it. Regardless of the original requirement, this is the scenario and it's driving me mad.
Without the first rule, the client enters into a request loop, trying to GET /index/X/Y/ repeatedly and getting 302 each time. The check on REDIRECT_STATUS makes everything run smoothly. But I would have thought that after the final rule, no more rules would be served, the client wouldn't make any more requests (note, no [R]), and everything would be gravy.
So... why would this result in a request loop when I take out the first rule?
Without being able to tinker with your setup, I can't say for sure, but I believe this problem is due to the following relatively arcane feature of mod_rewrite:
When you manipulate a URL/filename in per-directory context mod_rewrite first rewrites the filename back to its corresponding URL (which is usually impossible, but see the RewriteBase directive below for the trick to achieve this) and then initiates a new internal sub-request with the new URL. This restarts processing of the API phases.
(source: mod_rewrite technical documentation, I highly recommend reading this)
In other words, when you use a RewriteRule in an .htaccess file, it's possible that the new, rewritten URL maps to an entirely different directory on the filesystem, in which case the .htaccess file in the original directory wouldn't apply anymore. So whenever a RewriteRule in an .htaccess file matches the request, Apache has to restart processing from scratch with the modified URL. This means, among other things, that every RewriteRule gets checked again.
In your case, what happens is that you access /index/X/Y/ from the browser. The last rule in your .htaccess file triggers, rewriting that to /index.php?id=X&cat=Y, so Apache has to create a new internal subrequest with the URL /index.php?id=X&cat=Y. That matches your earlier external redirect rule, so Apache sends a 302 response back to the browser to redirect it to /index/X/Y/. But remember, the browser never saw that internal subrequest; as far as it knows, it was already on /index/X/Y/. So it looks to you as though you're being redirected from /index/X/Y/ to that same URL, triggering an infinite loop.
Besides the performance hit, this is probably one of the better reasons that you should avoid putting rewrite rules in .htaccess files when possible. If you move these rules to the main server configuration, you won't have this problem because matches on the rules won't trigger internal subrequests. If you don't have access to the main server configuration files, one way you can get around it (EDIT: or so I thought, although it doesn't seem to work - see comments) is by adding the [NS] (no subrequest) flag to your external redirect rule,
RewriteRule ^index\.php$ http://example.com/index/%1/%2/? [L,R,NS]
Once you do that, you should no longer need the first rule that checks the REDIRECT_STATUS.
The solution below worked for me.
RewriteEngine on
RewriteBase /
#rule1
#Guard condition: only if the original client request was for index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php [NC]
RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$ [NC]
RewriteRule . /index/%1/%2/? [L,R]
#rule 2
RewriteRule ^index/(\d+)/(\d+)/$ /index.php?id=$1&cat=$2 [L,NC]
Here is what I think is happening
From the steps you quoted above
Browse to index.php?id=3&cat=5
See the location bar read index/3/5/
Have the content served from index.php?id=3&cat=5
At Step 1, Rule 1 matches and redirects to location bar and fulfills Step 2.
At Step 3, Rule 2 now matches and rewrites to index.php.
The rules are rerun, for the reasons David stated, but since THE_REQUEST is immutable once set to the original request, it still contains /index/3/5 so Rule 1 does not match.
Rule 2 does not match either and the result of index.php is served.
Most other variables are mutable e.g. REQUEST_URI. Their modification during rule processing, and the incorrect expectation that the pattern matches are against the original request is a common reason for infinite loops.
Its feels quite esoteric sometimes, but I am sure there is a logical reason for its complexity :-)
EDIT
Surely there are two distinct requests
There are 2 client requests, the original one from Step1 and the one from the external redirect in step 2.
What I glossed over above is that when Rule 2 matches on the second request, it is rewritten to /index.php and causes an internal redirect. This forces the .htaccess file for / directory to be loaded again (it could easily have been another another directory with different .htaccess rules) and Re-run all the rules again.
So... why would this result in a request loop when I take out the first rule?
When the rules are re-run, the first rule now unexpectedly matches, as a result of Rule2's rewrite, and does a redirect, causing an infinite loop.
David's answer does contain most of this information and is what I meant "for the reasons David stated".
However, the main point here is that you do need the extra condition, either your condition, which stops further rule processing on internal redirects, or mine, which prevents rule 1 from matching, is necessary to prevent the infinite loop.

Resources