.htaccess mask/cloak image source URLs - apache

I hope you can help me.
I have the problem that I would like to mask URLs from another (not on my server) Domain
<img src="https://images2.externaldomain.com/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.otto.de%2Fi%2Fotto%2F60393181-9d98-5221-b0ab-35d38cd2aee2.jpg%3F%24Preset_Retargeting_640%24&feedId=62797&k=bd10410eeddf3ad8980f6e543edf4455110cb164">
to a masked URL like
<img src="https://mysubdomain.mydomain.com/productimages/bd10410eeddf3ad8980f6e543edf4455110cb164.jpg">
The image ID should the ID at the end k=
The problem is that I need a wildcard solution because I have a lot images with these URLs and only https://images2.externaldomain.com is stable. Every image has an ID (k=) in the source URL.
which RewriteRule or Condition could work?
many thank to every hint!
I added the snippet into my .htaccess without effect on my html. the sources of the images look exactly the same. how can I mask them that it look like image source of my server?
the hint of MrWhite was great and it works on https://htaccess.madewithlove.com/ but not on my site - what did I wrong?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)k=([\da-f]+)
RewriteRule ^$ productimages/%1.jpg [QSD,L]
</IfModule>
My images are look like:
<img src="https://images2.externaldomain.com/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.otto.de%2Fi%2Fotto%2F60393181-9d98-5221-b0ab-35d38cd2aee2.jpg%3F%24Preset_Retargeting_640%24&feedId=62797&k=bd10410eeddf3ad8980f6e543edf4455110cb164">
or
<img src="https://images2.externaldomain.com/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.otto.de%2Fi%2Fotto%2F06a05fc1-0554-5463-a8cf-6092a79214f0.jpg%3F%24Preset_Retargeting_640%24&feedId=56149&k=d27dd6627f26f5350af6afcec2c2afd71500c218">
in HTML code. Images are external on another server.
I would like to mask or cloak them with htts://mysubdomain.mydomain.com/productimages/d27dd6627f26f5350af6afcec2c2afd71500c218.jpg

To internally rewrite a URL of the form:
/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.test.com%2Fi%test%2F37b3895d-f743-4572-9017-6725903fef30.jpg%3F%24Preset_Retargeting_640%24&feedId=62797&k=4c8370f2e926de654b1f0a08530bc6065e6a80d3
to
/folder/4c8370f2e926de654b1f0a08530bc6065e6a80d3.jpg
In other words, capturing the value of the k URL parameter in the originally requested URL and use this as the file basename in the URL being rewritten to, then you can do something like the following using mod_rewrite near the top of the root .htaccess file (the order of directives can be important):
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)k=([\da-f]+)
RewriteRule ^$ folder/%1.jpg [QSD,L]
This ignores all other URL parameters and looks only for the k URL param. This param can occur anywhere in the query string.
([\da-f]+) - This regex matches the value of the k URL param, which looks like a hexadecimal string. In your example this is 40 characters long - if it is always 40 characters long then this could be limited in the regex as well. eg ([\da-f]{40}) - to only match hex strings that are 40 characters long.
Optionally, you can also test whether the target URL exists as a file before rewriting the request. For example, add the following condition (RewriteCond directive) after the first:
:
RewriteCond %{DOCUMENT_ROOT}/folder/%1.jpg -f
:

Related

htaccess url redirect with get parameters ID and reduce value

I want to do an url redirect to a new domain by retrieving the ID parameter but only taking the first 4 characters. Anyone know how to do this?
For example, an original url:
http://www.original.example/see/news/actualite.php?newsId=be9e836&newsTitle="blablabla"
To :
https://www.new.example/actualites/be9e
I have tested :
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
There are a couple of problems with this:
The regex ^$ in the RewriteRule pattern only matches the document root. The URL in your example is /see/news/actualite.php - so this rule will never match (and the conditions are never processed).
The regex ^newsId=(.*)$ is capturing everything after newsId=, including any additional URL parameters. You only need the first 4 characters of this particular URL param.
As an aside, your existing condition is dependent on newsId being the first URL parameter. Maybe this is always the case, maybe not. But it is relatively trivial to check for this URL parameter, regardless of order.
Also, do you need a case-insensitive match? Or is it always newsId as stated in your example. Only use the NC flag if this is necessary, not as a default.
Try the following instead:
RewriteCond %{QUERY_STRING} (?:^|&)newsId=([^&]{4})
RewriteRule ^see/news/actualite\.php$ https://www.new.example/actualites/%1 [QSD,R,L]
The %1 backreference now contains just the first 4 characters of the newsId URL parameter value (ie. non & characters), as denoted by the regex ([^&]{4}).
The QSD flag (Apache 2.4) discards the original query string from teh redirect response. No need to append the substitution string with ? (an empty query string), as would have been required in earlier versions of Apache.
UPDATE:
I have an anchor link (#) which is added at the end of the link, is there a possibility of deleting it to make a clean link? Example, currently I have: https://www.new.example/news/4565/#title Ideally : https://www.new.example/news/4565
The "problem" here is that the browser manages the "fragment identifier" (fragid) (ie. the "anchor link (#)") and preserves this through the redirect. In other words, the browser re-appends the fragid to the redirect response from the server. The fragid is never sent to the server, so we cannot detect this server side prior to issuing the HTTP redirect.
The only thing we can do is to append an empty fragid (ie. a trailing #) in the hope that the browser discards the original fragment. Unfortunately, you will likely end up with a trailing # on your redirected URLs (browser dependent).
For example (simplified):
:
RewriteRule .... https://example.com/# [R=301,NE,L]
Note that you will need the NE flag here to prevent Apache from URL-encoding the # in the redirect response.
Like I say above, browsers might handle this differently.
Further reading:
URL Fragment and 302 redirects
redirect is keeping hash
How to clear fragment identifier on 302 redirect?

.htaccess : Pretty URL with whatever number+names of parameters

Hello !
I know there already are a lot of topics about URL rewritting and I honestly swear I've spent a lot of time trying to apply them to my problem but I can't see any of them perfectly applying to my situation (if you find otherwise, please give the link).
-----
Here's the problem :
I'm learning MVC model and URL rewriting and I have my URL like this :
http://localhost/blahblahblah/mywebsite/index.php?param1=value1&param2=value2&param3=value3 ... etc ...
What I want (for some MVC template goals) is to have this kind of URL :
http://localhost/blahblahblah/mywebsite/value1/value2/value3 ... etc ...
-----
Whatever are the names of the parameters and whatever are the values.
This is the most essential thing I can't find a solution for.
(Also don't mind the localhost blahblahblah, this has to work even on distant websites but I trust it will work fine on online website has this part of URL may have no importance in what I want to do)
Thanks a lot for your time if you can help me seeing clearer in what I need to do.
If the .htaccess file is located in the document root (ie. effectively at http://localhost/.htaccess) then you would need to do something like the following using mod_rewrite:
RewriteEngine On
RewriteRule ^(blahblahblah/mywebsite)/(\w+)$ $1/index.php?param1=$2 [L]
RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)$ $1/index.php?param1=$2&param2=$3 [L]
RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)/(\w+)$ $1/index.php?param1=$2&param2=$3&param3=$4 [L]
# etc.
Where $n is a backreference to the corresponding captured group in the preceding RewriteRule pattern (1st argument).
UDPATE: \w is a shorthand character class that matches a-z, A-Z, 0-9 and _ (underscore).
A new directive is required for every number of parameters. You could combine them into a single (complex) directive but you would have lots of empty parameters when only a few parameters were passed (rather than not passing those parameters at all).
I'm assuming your URLs do not end in a trailing slash.
If, however, the .htaccess file is located in the /blahblahblah/mywebsite directory then then directives could be simplified a bit:
RewriteRule ^(\w+)$ index.php?param1=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?param1=$1&param2=$2 [L]
RewriteRule ^(\w+)/([\w]+)/([\w]+)$ index.php?param1=$1&param2=$2&param3=$3 [L]
# etc.
Don't use URL parameters (alternative method)
An alternative approach is to not convert the path segments into URL parameters in .htaccess and instead just pass everything to index.php and let your PHP script split the URL into parameters. This allows for any number of parameters.
For example, your .htaccess file then becomes rather more simple:
RewriteRule ^\w+(/\w+)*$ index.php [L]
(This assumes the .htaccess file is located in /blahblahblah/mywebsite directory, otherwise you need to add the necessary directory prefix as above.)
The RewriteRule pattern simply validates the request URL is of the form /value1 or /value1/value2 or /value1/value2/value3 etc. And the request is rewritten to index.php (the front-controller) to handle everything.
In index.php you then examine $_SERVER['REQUEST_URI'] and parse the requested URL.

How to allow special characters in Rewrite_Map?

Here is the config I am using
RewriteEngine on
RewriteMap shortlinks txt:/var/www/html/s.overhash.net/public_html/shortlinks.txt
RewriteRule ^/(.+)$ ${shortlinks:$1} [R=temp,L]
My txt document looks something like this:
9H40o https://osyrisrblx.github.io/playground/#code/HYUw7gBAggTjCGBPAPMArgWwEYhgPgAoBKAOhhABM0BjEAggB3IDcAaCatOEYAFyIC8eJiGYBqAZ258iQA
However, upon going to my site (http://s.overhash.net/9H40o), it replaces the # with %23, making the URL this:
https://osyrisrblx.github.io/playground/%23code/HYUw7gBAggTjCGBPAPMArgWwEYhgPgAoBKAOhhABM0BjEAggB3IDcAaCatOEYAFyIC8eJiGYBqAZ258iQA
(which isn't valid)
How would I go about ensuring the # remains?
It's not a rewrite map issue, it's the way rewrite rules work.
By default, special chars will be escaped.
Use the NE flag in your rewriterule if you don't want that to happen:
RewriteRule ^/(.+)$ ${shortlinks:$1} [NE,R=temp,L]
More details on https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_ne

modrewrite alter 1 element of query string

Flash movies are called based on dynamic links on mypage.php. mypage.php has the flash player embedded. The links look like mypage.php?moviefolder=folder1/folder2&swfTitle=sometitle.swf. mypage.php is parsed on each link click (per the href). Folder2 is always the same but movieTitle.swf is dynamic. Sometimes subfolders will be called (folder2/subfolder2/sometitle.swf).
Can mod_rewrite allow the query string to reflect folder2 but instead silently serve folder3 as well as occasional subfolders? I would place all files in folder3. The goal is to have the user not know where the swfs are. Thanks in advance again!
Using a RewriteCond to match the contents of the query string (since they are not read in a RewriteRule directive, you can extract swfTitle=sometitle.swf and substitute folder1/folder3 for folder1/folder2 in the moviefolder.
This will use a regex pattern like ([^&]+) to match everything up to the next & (which denotes another query param).
# Capture everything after folder2 into %1
RewriteCond %{QUERY_STRING} moviefolder=folder1/folder2([^&]+) [NC]
# Capture everything in the swfTitle param into %2
# Both conditions must be matched...
RewriteCond %{QUERY_STRING} swfTitle=([^&]+) [NC]
# Then silently rewrite mypage.php to substitute folder3,
# and pass in the original swfTitle captured above
RewriteRule ^mypage\.php$ mypage.php?moviefolder=folder1/folder3%1&swfTitle=%2 [L]
Hopefully, you won't get a rewrite loop, since the rewritten folder1/folder3 won't match the second time. [NC] allows for a case-insensitive match.
I did manage to successfully test this over at http://htaccess.madewithlove.be/, using the sample input:
http://example.com/mypage.php?swfTitle=thetitle.swf&moviefolder=folder1/folder2/thing
---> http://example.com/mypage.php?moviefolder=folder1/folder3/thing&swfTitle=thetitle.swf
http://example.com/mypage.php?moviefolder=folder1/folder2/thing999zzz&swfTitle=thetitle.swf
---> http://example.com/mypage.php?moviefolder=folder1/folder3/thing999zzz&swfTitle=thetitle.swf

Using mod_rewrite to put words in the URL instead of category IDs

I'm working on a Business Directory Website. What I'd like to do is transform the URL String of :
http://www.website.co.uk/business/results.php?category_id=11
To http://www.website.co.uk/business/Financial & Legal
How do I achieve this? Not too sure on the Rewrite Rules etc I am to use.
Thanks for any help, in advance.
I am now trying jut a test on a local host.
The HTML Code is as follows :
<h1>This is the PHP file.</h1>
Link Here
So all I want is the URL to be http://localhost/mod_rewrite/index.php/category
rather than : http://localhost/mod_rewrite/index.php?url=category
The .htaccess file is as follows (But doesnt appear to work)??
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?url=$1 [L]
Well,
in general you don't use ampersands in URLs.
They will be replaced by "%26" when you try to call them as a link.
But a general approach towards your question could look like that:
RewriteEngine on
RewriteRule ^business/Financial.*?Legal$ results.php?category_id=11 [L]