How to specify no file extension for Apache .htaccess' AddCharset? - .htaccess

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>

Related

How to gzip a .OBJ file is it possible in the .htaccess file?

Hi I couldn't find much online on how to gzip a .obj file I did manage to compress it changing the files extension to .gz but this isn't desirable in my case.
I want to gzip the .obj file as it's 13mb is this possible to do with the .htaccess file and how would one go about it? (keeping the .obj extension).
Thanks in advance!
Possible. Using mod_deflate module and modify the .htaccess as follow:
<ifmodule mod_deflate.c>
<ifmodule mod_mime.c>
Addtype text/plain .obj
</ifmodule>
AddOutputFilterByType DEFLATE text/plain
</ifmodule>
Before compression:
> curl --compressed -so /dev/null http://localhost/foo.obj -w '%{size_download}'
464590
After compression:
> curl --compressed -so /dev/null http://localhost/foo.obj -w '%{size_download}'
148261

simple "Up a directory" button in htaccess?

a while ago i was using htaccess to display some files, and recently i started that project again and found i had somehow deleted the "go up a level" button back then.
Can anyone tell me what the code line in htaccess looks like to get this button back? Should be relatively simple but i just cant find it... heres what i got.
Options +Indexes
# DIRECTORY CUSTOMIZATION
<IfModule mod_autoindex.c>
IndexOptions IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* SuppressHTMLPreamble
# SET DISPLAY ORDER
IndexOrderDefault Descending Name
# SPECIFY HEADER FILE
HeaderName /partials/header.html
# SPECIFY FOOTER FILE
ReadmeName /partials/footer.html
# IGNORE THESE FILES, hide them in directory
IndexIgnore ..
IndexIgnore header.html footer.html icons
# IGNORE THESE FILES
IndexIgnore header.html footer.html favicon.ico .htaccess .ftpquota .DS_Store icons *.log *,v *,t .??* *~ *#
# DEFAULT ICON
DefaultIcon /icons/generic.gif
AddIcon /icons/dir.gif ^^DIRECTORY^^
AddIcon /icons/pdf.gif .txt .pdf
AddIcon /icons/back.png ..
</IfModule>
Options -Indexes
Okay found the problem, it was simple, just not very observant when looking at the code. The line "IndexIgnore .." roughly in the middle.

disable .php and .html files from my directory listing?

How can I disable .php and .html files from my directory listing?
When someone visit my directory
http://example.com/dir
All other files except .php and html should be listed.
Add the following line to your .htaccess file
IndexIgnore *.html *.php
This tells the Apache web server to list all files except those that end with .php and .html .
If you want to disable all files and directories form listing ,Add this line to your .htaccess
IndexIgnore *
The wildcard '*' means it will not display any files

Canonical Header Links for PDF and Image files in .htaccess

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

Mime type for rss file generated by php?

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

Resources