.htaccess rule stacking and 404 redirect - .htaccess

The user requests a PNG image, say: http://server/myfolder/subfolder/1234.png. If that .png doesn't exist, then I want to show instead a .gif in the same folder of the same name except for the .gif extension, which should exist.
Another addition is that we recently changed the directory structure so the URL that I need to check may have changed. For example, the URL above should be able to be reached by requesting either directly as http:// server/myfolder/subfolder/1234.pngor by http://server/oldfolder/1234.png. The change in the directory structure can be expressed as:
RewriteRule oldfolder/(.*) myfolder/subfolder/$1 [L,QSA]
In both directories, I need to check if the file exists, and use a different image instead if necessary. Any help would be greatly appreciated.

Try adding the following to the .htaccess file in the root directory of your site.
It should work with your changed file structure too.
RewriteEngine on
RewriteBase /
#if the file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
#and is in any of these folders: /myfolder/subfolder or /folder1 or /folder2
RewriteCond %{REQUEST_URI} ^/(myfolder/subfolder|folder1|folder2)/ [NC]
#and its a png, then try to serve the gif instead
RewriteRule ^(.+)\.png$ $1.gif [L,NC]

Related

removed .html extensions with htaccess now index.html give 403 error

After entering the code below, my home page gives a 403 error. The rest of the site works perfectly. All instances of .html were removed.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Any advice?
Thank you!
example.com leads ti the 403 error. If I write example.com/index it works fine.
Something else must have changed for this to result in a 403 error. The code you posted won't actually do anything when you request example.com/ - the same as if that code didn't exist at all. (UPDATE: However, this assumes your .htaccess file is located in the document - it appears this is not the case - see below.)
However, what will trigger a 403 in such cases is when "formatted directory listings" are disabled and the directory index document cannot be found (or has been disabled).
So, try setting the appropriate directory index at the top of your .htaccess file:
DirectoryIndex index.html
It is the DirectoryIndex that serves the appropriate file when requesting your "home page", not your directives in .htaccess.
UPDATE:
It [.htaccess] is located in my root directory. Would it be better to put it in the public_html folder?
Yes, the code you posted should go in the /public_html directory (ie. your document root). If these directives are in a .htaccess file above the document root then the RewriteRule pattern will match the URL-path public_html/ and rewrite the URL to public_html/.html which is possibly where your 403 error is coming from ("dot" files are usually hidden/protected OS files and you may also have a directive in your server config blocking access. However, this behaviour may also be dependent on other factors in the server config/OS). However, with that code in the document root then a request for example.com/ (your home page) won't be processed by these directives (which is good) - mod_dir should then serve the index.html file in this instance.
However, you don't want to process "directories" anyway (public_html is obviously a "directory", not a file). Which is what's happening above. eg. .html shouldn't be appended to public_html/ to begin with (or example.com/path/to/directory/ or any other directory). This can be avoided by adding an additional condition to your rule block to avoid directories (as well as files). For example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.html [L]
Simply adding that additional RewriteCond directive might be enough and still allow you to keep your .htaccess file above the document root. (However, you may still need to move the .htaccess file as well, as described above.)
Also, the NC flag is not required here and literal dots don't need to be escaped when used inside a character class.
You could also extend this code to first check the existence of the file (with a .html extension) before rewriting, although this may be unnecessary in your case. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.]+)$ $1.html [L]
This requires an additional "file check" which may be an unnecessary overhead.

mod_rewrite change extensions .gif/.jpg to .png

The rule I have been fruitlessly working with doesn't work, I am trying to get all images in 1 directory to load as .png The images in the directory are a mixture of .png, .jpg and .gif
I want to be able to load the file tree.jpg by going to tree.png (no files have the same name). I am sure my mistake is obvious or my entire attempt is wrong, I just can't work it out.
The htaccess file is in the same folder with the images, which is called /thumbs
RewriteEngine On
RewriteRule ^([^.]*)\.gif$ /thumbs/$1.png [R,L,NC]
I tried this also, but it just givens a broken link to both .gif and .png versions
RewriteEngine on
RewriteRule ^(.+)\.gif$ $1.png
I tried this too, but it adds in my server path to the URL for some reason
RewriteEngine on
RewriteRule ^([^.]*)\.gif$ $1.png [R,L,NC]
You can use this rule:
RewriteEngine on
RewriteRule ^([^.]+)\.png$ /$1.gif [L,NC,R]

.htaccess redirecting requests to the same folder

I want to use .htaccessto redirect different requests to the same folder.
E.g.:
domain.de/ordner1/fileX.html
domain.de/en/folder1/fileX.html
domain.de/it/casella1/fileX.html
So whenever something is requested out of /ordner1/, /folder1/ or /casella1/ I want .htaccess to fetch the requested file out of a specific directory like domain.de/all/fileX.html.
I want to prevent duplicate content but also keep the foldernames in the selected language.
Could you help me solve this problem?
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#skip css, js etc
RewriteCond %{REQUEST_URI} !\.(css|js)[NC]
#if request to ordner or folder1 or casella1, serve the file from all/
RewriteRule ^(ordner1|en/folder1|it/casella1)/(.+)$ all/$2 [L,NC]
In your docroot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(ordner1/|en/folder1/|it/casella1/)(.*$) all/$2 [L]
You would need to add extra names to map other translation equivalents.

Moves website to subdirectory - Getting images/css paths to work?

Ive got a website in a subdirectory but all the image, link and CSS paths are set like this:
"/images/error.png"
or "/help.html"
or "/css/styles/stylesheet.css"
I've tried a million different .htaccess things and tried setting the base href tag to the domain.com/subfolder.
I can't seem to get anything to work!
Also: There are hundreds of files in this system so I can't manually go and change all the paths to relative.
When you wrote: Ive got a website in a subdirectory I am assuming that you moved all images, css etc to subdirectory as well.
If that is so you can manage it using a simple RewriteRule. Create a .htaccess under DOCUMENT_ROOT with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# if this is a link
RewriteCond %{REQUEST_FILENAME} -l
# forward to subdirectory if it is not already subdirectory
RewriteRule ^((?!subdirectory/).*)$ subdirectory/$1 [L,NC]
Use Relative Paths (http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm)
So if your images are in
/SubFolder/Images/
and your viewing the file
/SubFolder/index.htm
Images/fileName.png
Will work.

htaccess, a shortened url should not match a file

I have a little problem with my apache2 and .htaccess rules.
for example:
I have a shortened uri like
www.domain.tld/sitemap
which has to be rewritten by a rewriterule, redirected in a php File to display the sitemap.
The problem is, that in the root folder a file named sitemap.xml exists.
My apache automatically calls the sitemap.xml file but i don`t want that.
The file should be only called when uri is
www.domain.tld/sitemap.xml
is there a possibility to avoid the call of this file when the shortened URI is called?
this is just an example. There are some files that are required to be in the root folder and can`t moved from there into a subfolder (which would be the easiest way to fix this problem, but its not possible in my situation). it is required that these files are callable by uri.
Has anyone an idea how to fix this problem?
my current .htaccess file
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php?urlseg=%1&%{QUERY_STRING} [NC,L]
Thanks a lot!
You likely have MultiViews enabled, which auto-resolves your non-existent resource /sitemap to the existent resource /sitemap.xml. Especially in cases where you're using mod_rewrite, I really see no need for MultiViews, so you can turn it off by adding this to the top of your .htaccess file:
Options -MultiViews
Doing so should hopefully prevent this from happening.

Resources