htaccess hide "Index of" - .htaccess

I want display just the directory content of my Webserver.
My .htaccess file contains:
Options '+Indexes'
IndexOptions +FancyIndexing
And in the browser I got the path :
"Index of /~efc/"
In the footer :
"Apache/2.2.22 (FreeBSD) mod_ssl/2.2.22 OpenSSL/1.0.1c mod_hcgi/0.9.4 DAV/2 Server"
Is there a way to prevent these lines?
EDIT: In addition to the answer of Sàt i got my desired result with following directives:
Options '+Indexes'
# SET INDEX OPTIONS
IndexOptions IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* SuppressHTMLPreamble
# SPECIFY HEADER FILE
HeaderName header.html
# SPECIFY FOOTER FILE
ReadmeName footer.html
# IGNORE THESE FILES
IndexIgnore header.html footer.html

Sure. Use the HeaderName and ReaderName directives, like this:
Options '+Indexes'
IndexOptions +FancyIndexing
HeaderName fancy_hdr.html
ReadmeName fancy_readme.html
HeaderName points at the html file rendered before the listing, ReadmeName points at the html rendered after the listing.
There are a whole lot of fun customization options, you might want to take a look at those in the docs or various tutorials.

The footer is also printed on error pages like 404. You can control its display via the ServerTokens directive in your httpd.conf.

Related

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

How change the pagination url in .htaccess

i am new to .htaccess an i want to know how to change the following url.
fun.php?search=&submit=search&page=2
Int to this
fun/page/2
I think it cannot be done in a direct way. Php GET method is needed to pass variables to the php code. However, you can add "fun" directory,add "page" directory, add "2" directory and add 2.php into your page directory with this code
<?php
header('Location:http://yourweb/fun.php?search=&submit=search&page=2');
?>
add a .htaccess file into "2" directory with this code
DirectoryIndex 2.php
That's all. Remember, php get method variable is not mean't to be shortened. Instead, use POST method
Hope it works

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

.htaccess override IndexIgnore *, IndexIgnore nothing

I have a directory with an .htaccess file having the setting IndexIgnore *. How can I set a subdirectory to not ignore anything, or to ignore the negation of everything, so as to override the parent directory's IndexIgnore? Like IndexIgnore ^*, only a working version thereof?
Simply add IndexIgnoreReset ON before the new IndexIgnore directive.
And then, to show everything in the sub folder use IndexIgnore "."
Check the documentation at:
IndexIgnoreReset
I don't think it will be possible because IndexIgnore list is always appended in the existing list (from parent .htaccess). There is now way to overwrite this directive AFAIK.
In <Directory> block you can add:
IndexIgnoreReset ON
IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
This will override config for this specific <Directory>

Resources