My site, parsing image from another site, but don't show them. (It shows: ����������������� )
I know what it is, it is a problem with Content-Type.
I need to use Content-Type: image/jpeg, and all will be ok!
But images are opened by URL (eg. example.com/DIR1/(100000-999999)/DIR2/(100000-999999)/). This number is random.
If I write in .htaccess (on example.com) the following then it works:
Header set Content-Type: image/jpeg - i see photo .
But then all other URLs fail!
How can I : If DIR1/(.*)/DIR2/(.*)/ - Use Content-type: image/jpeg ?
DIR1 , DIR2 = Photos/(number)/image/(number)
Your script that serves the image should really be generating this header as part of the response.
However, to set this conditionally in .htaccess just for this form of URL-path then you could do something like this:
# Set env var if required URL-path is requested
SetEnvIf Remote_URI "^/DIR1/\d+/DIR2/\d+/$" SET_JPG_CONTENT_TYPE=1
# Only set header when env var is set
Header set Content-Type "image/jpeg" env=SET_JPG_CONTENT_TYPE
Related
In https://stackoverflow.com/a/25255559/1118719 we see
AddCharset utf-8 .html .css .php .txt .js
That is marvelous for files named bla.html, bla.css, etc.
But how to match just files named bla?
Sure, one could try e.g.,
AddCharset utf-8 .html .txt ""
but that doesn't work.
Yes, maybe there is no solution for 'bla' and 'bla.'.
Alas, one must resort to e.g.,
<FilesMatch "^[^.]+$">
ForceType 'text/plain; charset=UTF-8'
</FilesMatch>
i'm using Nginx on production env, we have some url with double slash in uri, like this:
http://foo.com//bar/foo
i want to rewrite or redirect to:
http://foo.com/bar/foo
the option " merge_slashes " it's already enabled, and i have tried this:
merge_slashes off;
rewrite (.*)//+(.*) $1/$2 permanent;
but doesn't works, have you any idea?
thanks
EDIT:
so, the rewrite work if the slash is after the first "/" , i.e:
http://pippo.it/foo//bar
but if the double slash it's after the domain doesn't work, i.e:
http://pippo.it//foo/bar
i have tried to dump the variable $request_uri and this is the result:
url: http://pippo.it//foo/bar
expect: $request_uri -> //foo/bar
result: $request_uri -> /foo/bar
url: http://pippo.it///foo/bar
expect: $request_uri -> ///foo/bar
result: $request_uri -> //foo/bar
can be a bug?
EDIT 2:
i have found the error, the problem is the elb (aws), if i call directly the ec2 instance (where nginx it's installed) i can see the first 2 slashes, but if i call the elb, the load balancer delete the first slash, i have opened a case on the support center.
EDIT 3:
found the issue: if you set the elb with HTTP listener, i dont know why but the request doesn't have all slashes, if you set the listener in TCP mode, the request works fine.
Thanks
Setup is actually correct. Tested it with nginx/1.9.12
root#8317e542a878:/etc/nginx/sites-enabled# curl -I localhost//
HTTP/1.1 301 Moved Permanently
Server: nginx/1.9.12
Date: Mon, 18 Jul 2016 20:34:29 GMT
Content-Type: text/html
Content-Length: 185
Location: http://localhost/
Connection: keep-alive
Have you reloaded settings on running nginx?
nginx -s reload
Give this a try:
echo 'http://foo.com//bar/fool' | sed 's/\/\//\//g'
This is using the sed unix command to search for // and replace them with /.
I'm attempting to setup Canonical links for a number of PDF and images files on my website.
Example Folder Structure:
/index.php
/docs/
file.pdf
/folder1/
file.pdf
/folder2/
file1.pdf
file2.pdf
/img/
sprite.png
/slideshow/
slide1.jpg
slide2.jpg
Example PDF URL to Canonical URL:
http://www.example.com/docs/folder1/file.pdf --> http://www.example.com/products/folder1/
I am trying to avoid having to put individual .htaccess files in each of the sub-folders that contain all of my images and PDFs. I currently have 7 "main" folders, and each of these folders have any where from 2-10 sub-folders, and most sub-folders have their own sub-folders. I have roughly 80 PDFs, and even more images.
I'm looking for a (semi)dynamic solution where all files in a certain folder will have the Canonical Link set to a single url. I want to keep as much as possible in a single .htaccess file.
I know that <Files> and <FilesMatch> do not understand paths, and that <Directory> and <DirectoryMatch> don't work in .htaccess files.
Is there a fairly simple way to accomplish this?
I don't know of a way to solve this with apache rules alone as it would require some sort of regex matching and reusing the result of the match in a directive, which isn't possible.
However, it's pretty simple if you introduce a php script into the mix:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|png|pdf)$
RewriteRule (.*) /canonical-header.php?path=$1
Note that this would send requests for all jpg, png and pdf files to the script regardless of the folder name. If you want to include only specific folders, you could add another RewriteCond to accomplish that.
Now the canonical-header.php script:
<?php
// Checking for the presence of the path variable in the query string allows us to easily 404 any requests that
// come directly to this script, just to be safe.
if (!empty($_GET['path'])) {
// Be sure to add any new file types you want to handle here so the correct content-type header will be sent.
$mimeTypes = array(
'pdf' => 'application/pdf',
'jpg' => 'image/jpeg',
'png' => 'image/png',
);
$path = filter_input(INPUT_GET, 'path', FILTER_SANITIZE_URL);
$file = realpath($path);
$extension = pathinfo($path, PATHINFO_EXTENSION);
$canonicalUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . dirname($path);
$type = $mimeTypes[$extension];
// Verify that the file exists and is readable, or send 404
if (is_readable($file)) {
header('Content-Type: ' . $type);
header('Link <' . $canonicalUrl . '>; rel="canonical"');
readfile(realpath($path));
} else {
header('HTTP/1.0 404 Not Found');
echo "File not found";
}
} else {
header('HTTP/1.0 404 Not Found');
echo "File not found";
}
Please consider this code untested and check that it works as expected across browsers before releasing it to production.
I was able to achieve adding canonical links for files in different directories through a single .htacess file.
The following code adds a canonical link for each file pointing to the same directory:
<FilesMatch "\.(jpg|png|pdf)$">
RewriteRule ([^/]+)\.(jpg|png|pdf)$ - [E=FILENAME:%{HTTP_HOST}/<your-desired-location>/$1.$2]
Header add Link '<https://%{FILENAME}e>; rel="canonical"'
</FilesMatch>
And the code below adds a canonical link to the file's requested URL, which in many cases will be its actual location on the server:
<FilesMatch "\.(jpg|png|pdf)$">
RewriteRule ([^/]+)\.(jpg|png|pdf)$ - [E=FILENAME:%{HTTP_HOST}%{REQUEST_URI}]
Header set Link '<https://%{FILENAME}e>; rel="canonical"'
</FilesMatch>
Here is the solution !!!
you can use .htacess file for controlling header which is more simple way to manage headers.
How you can do ?
Lets take a example, I have a pdf named "testPDF.pdf" which is in the root folder of my site.
All you have to do, pasted following code into .htaccss file.
<Files testPDF.pdf >
Header add Link '<http://<your_site_name>.com/ >; rel="canonical"'
</Files>
Once you've added that to your .htaccess file, you'll need to test your header to ensure that it's working accurately
For an IIS solution, try something like this.
Response.AppendHeader("Link", "<" + "https://" + Request.Url.Host + "/" + product.GetSeName() + ">; rel=\"canonical\"");
this was added to a function which generated a PDF version of the webpage :)
I use php to auto generate an rss file from a database by putting some php commands in the rss file then changing the htaccess file in the rss directory so that the xml file will be parsed for php like this
AddType application/x-httpd-php .php .xml
But when i go to validate my feed it says the feed is valid but is being sent as a text file and I should change the htacces to include AddType application/xml. But when I add that after the php line, then the file won't execute the php commands in the file
What's the best solution for this?
try
header('Content-Type: application/rss+xml;charset= utf-8 ');
You have to set the Content-Type in your PHP code. Add this for example to your PHP code :
header('Content-Type: application/rss+xml');
Add:
header('Content-Type: application/rss+xml');
to your code. Also I'd suggest to write your XML instead of generating it on the fily. I pretty much doubt it changes so frequently.
You should add at the top of the file:
header('Content-type: application/atom+xml');
before send any content
So let's say:
- .htaccess
* assets
|> images
|> logo.png
|> css
|> style.css
|> home.css
How can i set the expires header for the whole assets folder and its contents?
I know that i can set it by type like:
ExpiresByType text/javascript "modification plus 2 hours 45 minutes"
But what about for a whole directory?
At the .conf level, use a <directory> directive:
<Directory /path/to/your/assets/folder>
ExpiresDefault ...
</Directory>
If you have only .htaccess control, then put a .htaccess into the assets folder with the same ExpiresDefault directive