how to Read images from different directory using htaccess - .htaccess

im looking for correct htaccess code to allow me to call images in my html file from
images_20170819/
images_20170820/
images_20170821/
however have it read them from
images/
thanks for any insight !

RewriteCond %{REQUEST_URI} ^/images/(.+)
RewriteCond %{DOCUMENT_ROOT}/images_20170819/%1 -f
RewriteRule ^/?images/(.+)$ /images_20170819/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/images/(.+)
RewriteCond %{DOCUMENT_ROOT}/images_20170820/%1 -f
RewriteRule ^/?images/(.+)$ /images_20170820/$1 [QSA,L]
You can check if the file exists for each image directories. For line 1 - line 3, if the request uri is starting with /images/, then check if the file requesting is inside the folder images_20170819, %1 is rewritecond backreference to (.+). If the file exists, rewrite the url internally to request the folder /images_20170819/. $1 is the rewriterule backreference to (.+).

Related

.htaccess rewrite requests to a subdirectory if they exist, otherwise redirect to a subdomain

I want something very specific to happen with my .htaccess file, but I'm not sure if it's possible. I want links like example.com/ExampleFile.txt to be forwarded to example.com/Other/ExampleFile.txt (as I'm about to move everything into the "Other" directory to do a cleanup of the root directory.) Then if no file is detected in the "Other" directory, I'd like the path that the user originally typed (example.com/ExampleFile.txt) to be sent through to subdomain.example.com/ExampleFile.txt.
Please let me know if this is possible, and if so, what code do I need to add to my .htaccess file? Note that I use LiteSpeed, not Apache.
I can already do the last part with the following piece of code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://subdomain.example.com%{REQUEST_URI} [R=302,L]
Add the following rewrite before your existing redirect to test whether the request maps to a file in the /Other subdirectory before rewriting the request if it is:
# Rewrite request to the "/Other" subdirectory
# - only if the request maps to a file or directory there
RewriteCond %{DOCUMENT_ROOT}/Other/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/Other/$1 -d
RewriteRule (.+) Other/$1 [L]
NB: If the same file exists in both the root (or rather, outside of the "/Other" subdirectory) then the one inside the /Other subdirectory wins.
If you only want to rewrite actual files and not directories then remove the second condition and OR flag.
Presumably all requests to the root should be rewritten to /Other/ (since this exists as a directory) so this should be performed unconditionally:
# Rewrite root to "/Other/"
RewriteRule ^$ /Other/ [L]
And your existing redirect to subdomain.example.com follows these rewrites.
UPDATE:
But I did notice that I can't access files without the file extensions using this method. [...] Any ideas why I can't access files without the extension when using this method? I have a file called ExampleFile.txt in /Other which can be seen at example.com/ExampleFile.txt but not example.com/ExampleFile.
Because we are having to check whether the requested URL maps to a file (or directory) in the subdirectory before rewriting the URL.
If you insist on having extensionless URLs for different types of resources (.txt, .html, images, etc.) then you will need to manually check each file extension for which you permit to be extensionless (in much the same way as you have already done for requests outside of the stated subdirectory).
For example:
# For files that already have an extension OR directories...
# NB: Directories could be requested initially with or without the trailing slash
# Rewrite request to the "/Other" subdirectory
# - only if the request maps directly to a file or directory there
RewriteCond %{DOCUMENT_ROOT}/Other/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/Other/$1 -d
RewriteRule (.+) Other/$1 [L]
# Check for ".txt" files...
RewriteCond %{REQUEST_URI} !(\.\w{2,4}|/)$
RewriteCond %{DOCUMENT_ROOT}/Other/$1.txt -f
RewriteRule (.+) Other/$1.txt [L]
# Check for ".html" files...
RewriteCond %{REQUEST_URI} !(\.\w{2,4}|/)$
RewriteCond %{DOCUMENT_ROOT}/Other/$1.html -f
RewriteRule (.+) Other/$1.html [L]
# Check for ".php" files...
RewriteCond %{REQUEST_URI} !(\.\w{2,4}|/)$
RewriteCond %{DOCUMENT_ROOT}/Other/$1.php -f
RewriteRule (.+) Other/$1.php [L]
# Check for ".jpg" files...
RewriteCond %{REQUEST_URI} !(\.\w{2,4}|/)$
RewriteCond %{DOCUMENT_ROOT}/Other/$1.jpg -f
RewriteRule (.+) Other/$1.jpg [L]
# etc.

htaccess file - how do these redirects work?

it's really difficult to understand htaccess's work, I have this code:
#Remove slash on the end
RewriteRule ^(.*)/$ $1 [R=301]
#Don't use .php extension in URL
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php
#All the directories should be redirected to index.php
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . index [R=301]
Structure of my root folder: index.php, .htaccess and dir directory with test.php file inside.
I know that this file re-executes multiple times until the changes in URL are over, so if I'm writing mysite.ru/index/dir/ to address bar for example (I use existing index file as directory, otherwise, if I use words that aren't related to existing file names, it's works), I'm expecting 404 page (Slash removes, dir.php doesn't exist, index/dir doesn't exist, file executes again, URL didn't changed and it's over), but I'm getting 500 error and 10 redirects in error.log file. Something wrong with .htacess file, and I can't understang what.
You can use END flag or RewriteCond %{ENV:REDIRECT_STATUS} ^$ condition to prevent the infinite rewrite loop error.
END flag works on Apache version 2.4 or greater
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [END]
The following condition works on all versions
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php
The reason why you got internal server error (Too many redirects) is because your rule rewrites the same request multiple times. Your rule gets triggered when you request /filename/blabla .
Since the /filename/blabla doesn't map to an existing file your condition RewriteCond %{REQUEST_FILENAME}\.php -f only checks the first path segment /filename . the condition is met and the rule gets triggered in loop.

Rewrite URL to remove query string

I have a URL that needs to be reformatted. example.com/?article_category=lifestyle I want this to be formatted to: example.com/lifestyle. However, when I try to reformat it with my .htaccess file it doesn't work. My .htaccess file is located in the same directory as my file. The file name is index.php.
Here's the rewrite rule I proposed:
RewriteRule ^([\w-]+)/?$ index.php?article_category=$1 [QSA,L]
I don't know why this isn't working. What am I doing wrong?
Useful Docs: RewriteCond Directive & RewriteRule Directive
RewriteCond %{QUERY_STRING} article_category=([a-z]+)
RewriteRule ^/?$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+) index.php?article_category=$1 [QSA,L]
Line 1: checking if query string containing article_category=([a-z]+), article_category=lifestyle matched
Line 2: if the URL is example.com or example.com/, redirect permanently to /lifestyle, %1 is a RewriteCond backreference, grouped parts (in parentheses ([a-z]+)) of the pattern
Line 3 / 4: if the URL is not regular files and directories
Line 5: if the URL is starting with alphabetic, rewrite the URL to index.php?article_category=$1

Rewrite Rule to redirect /dir or /dir/ to /dir/index.html where dir can be anything

I know apache has the default file to load when the request is a directory, but I want to have it redirect to the actual index.html file so my URL says /dir/index.html rather than /dir/
Thanks
This three rules should do its job:
RewriteCond -d %{REQUEST_URI}
RewriteCond -f %{REQUEST_URI}/index.html
RewriteRule ^(.+)/$ $1/index.html [l]
The first line checks if the uri is a directory the second checks if there is inside a index.html and if so redirect the user to this file.
And for php files this two lines:
RewriteCond -d %{REQUEST_URI}
RewriteCond -f %{REQUEST_URI}/index.php
RewriteRule ^(.+)/$ $1/index.php [l]

htaccess rewriterule

A fairly basic htaccess question, but I don't use it much, so i have no idea.
I want to check if the requested file exists. If it does, forward to one page, and if not forward to another page and pass the requested path as GET.
Thanks in advance.
RewriteEngine On
# check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# if not, pass it to index.php
RewriteRule ^(.*) index.php?page=$1 [QSA]
As Gumbo suggested, you can repeat the condition without the ! if you also want to rewrite the URL when a file exists. Maybe you want to 'protect' your real files and folders with this method.
Use the -f expression in a RewriteCond condition to test if the given path points to an existing regular file:
RewriteEngin on
# file exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ one-page [L]
# file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ another-page [L]
The initial requested URI should then be available in an environment variable.

Resources