.htaccess allow image to display directly - php

I have many files including images, php& js scripts and subfolders.
I need to redirect to index.php for any uri using .htaccess.
for example:
www.example.com/test should be redirect to index.php
But images should not be redirected to index.php. Instead it should be printed directly in browser.
For example:
www.example.com/sample.jpg should display sample.jpg.
I am using following codes in .htaccess code:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Can someone explain how to allow image to display?

Add conditions before your rule to skip image/css/js filesdirectories:
RewriteEngine On
RewriteRule !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ index.php [L,NC]

Related

How to redirect sub-directory to root directory with parameter as sub-directory using .htaccess

Want to Redirect to index.php
When user try to come at site using any URL like:
1)www.domain.com/xyz
2)www.domain.com/abc
3)www.domain.com/apple
4)www.domain.com/123
5)www.domain.com/hello
Whatever random text at the end of the URL.
I want .htaccess code which redirects those URL to
www.domain.com/index.php
index.php contain code to get data using GET method so the last string of URL needs to get in index.php file.
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/index.php?parameter=$1
I tried this code but it goes in infinite redirect.
I want to stop loop while parameter found.
You should be redirecting to a relative address (assuming that index.php is on the same domain). Also, you need to exclude existing files:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Redirect all subfolders to a file in root

There are similar situations and answers here to redirect a url to a specific file using mod-rewrite. I tried them but none of them worked for me.
I would like to redirect all requests to a file and handle everything by that file. It worked fine somehow, but when there are virtual sub-folders, it does not work at all.
For example:
http://test.com/this_page_does_not_exist.php
works fine. but when I use something like this:
http://test.com/no_sub_folder_here/this_page_does_not_exist.php
it does not work fine. It tries to find css files for 404 page from no_sub_folder_here. So what I need to have is to address
http://test.com/no_sub_folder_here
to
http://test.com/
and then load the css for the 404 page. my css is in the root.
Here is my .htaccess file
RewriteEngine On
# Determine the RewriteBase automatically/dynamically
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-f
# forward it to 404.php in current directory
RewriteRule . %{ENV:BASE}/404.php [L]
it does not redirect or change the directory from sub-directories inside 404 to root. It renders the codes, but the page style is all wrong because of missed css from
test.com/style.css
it checks
test.com/no_sub_folder_here/style.css
instead.
Also, after a couple of tries, it does not change the url form the no_sub_folder to root. So, after a while, I will get a long list of no_sub_folder in my url :(
Because you change the base path, you have to fix that, with:
<base href="http://test.com/">
in html <header> of your 404.php page.

php - htaccess: redirect .png to .php page and display html

I have a folder in the root called picture and I want to redirect (applying only to this folder) all the .png urls to:
/picture/index.php
displaying the index.php html content. I mean redirect urls like:
/picture.png, /picture/whatever.png, etc
Actually this is my .htaccess file inside the picture folder:
RewriteEngine On
RewriteRule ^/picture/(\w+)\.png$ /picture/index.php [L]
but if I go to domain.com/picture.png it returns 404
You can use this rule in /picture/.htaccess
RewriteEngine On
RewriteBase /picture/
RewriteRule \.png$ index.php [L,NC]

Htaccess Redirect for API

I want to redirect every .htaccess call to a particular file.
I want to redirect all Get/Put/Post/Delete calls to a folder to a specific index.php file.
I have following directory structure:
\var\www\html\app
inside app i have a file index.php
Now i want any calls like
GET http(s)://myWeb.com/app
POST http(s)://myWeb.com/app
GET http(s)://myWeb.com/app/helloworld
to redirect to
\var\www\html\app\index.php
Here is my failed attempt:
RewriteEngine On
RewriteBase /app/
RewriteRule ^ app/index.php [QSA,L]
#RewriteRule ^ index.php [QSA,L] //Tried this one as well
This .htaccess is inside
/var/www/html
I also placed a similar htaccess inside app folder but it doesnt works too.
Should i use 2 htaccess files ? If one what should be the format for it ?
Also, is there a tool to check htaccess regex ? something like www.regexr.com
Did you try this?
RewriteRule . app/index.php [R,L]
I think QSL is needed only if you want to pass the URL queries

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

Categories