Why RewriteCond is skipped if file not found? - .htaccess

Let's suppose I have something like this in my .htaccess file :
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule . index.php [QSA,L]
And there is only one image "abc.jpg" in the folder "images".
If I go to "www.mydomain.com/images/abc.jpg" the image will show up.
But if I go to "www.mydomain.com/images/123.jpg" this link will be rewrited to the file "index.php".
Is it normal? Is there a way to avoid this and have the default 404 browser page?

Related

htaccess rule to not redirect to the folder

Having an issue here while trying to remove .html extension from the url. I have work.html file and work folder on the same level. I'm tying to do rewrite in htaccess file, so it would work this way:
if someone hits URL .com/work -> it would load work.html page from the root directory.
if someone hits URL .com/work/blabla -> it would load blabla.html page from work directory (.com/work/blabla.html)
But at the minute, when someone hits .com/work browser redirects to .com/work/ (work folder) and shows forbidden message. All other pages works as expected, the only issue is with the file and folder having the same name.
My rules are:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Could anyone help me with this?
ADDITION:
On the picture below is file structure
file structure
after adding rewrites all works:
mywebsite.com -> show index.html
mywebsite.com/contact -> show contact.html
mywebsite.com/work/avrica - > show /work/avrica.html
the issue I have:
mywebsite.com/work -> which supposed to show work.html, but in reality it shows work folder with message forbidden
You can just use this single rule in your site root .htaccess:
DirectorySlash Off
Options -Indexes
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

URL rewrite in subfolder css not working

How are you today,
I have a problem in url rewriting..
I Upload my website on pesco sub-directory, Like www.example.com/pesco and my css path like this /css/style.css but when site run css must be like www.example.com/pesco/css/style.css but my css showing like this www.example.com/css/style.css url rewrite missed the pesco sub-directory
Please any one give me the solution
My Code is below you can see.
ErrorDocument 404 /notfound.php
RewriteEngine on
RewriteBase /pesco/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html index.php
RewriteRule ^aboutus.html aboutus.php
RewriteRule ^([a-zA-Z0-9_-]+).html$ products.php?catid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ detail.php?category=$1&product=$2
I think your error is just a reference problem. You can try ./css/style.css as this means it looks to find css as a directory inside the same directory as the file requesting it, whereas /css/style.css is essentially looking in the root directory

404 redirect using .htaccess

I want to set up a rule in my .htaccess file so that any url that is enetered, that results in a 404 because there is no such file, automatically re-directs to the home page of the site:
index.php
my .htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^queenslandbeerweek.com.au$ [NC]
RewriteRule ^(.*)$ http://queenslandbeerweek.com.au/$1 [L,R=301]
ErrorDocument 404 /index.php
This causes the index.php file to show but is broken and leaves the eroneous URL in the address bar.
I have read in the answer to another post that it has something to do with passing the erroneous URL as a parameter, causing the page to not load properly, because the page calls data from a database and it is passing the bad URL as a parameter of index.php but there was no hint as to what the solution is.
What I would like to happen, is if an incorrect URL is typed into the address bar, or if a link is followed, to a file that does not exist, the completely forget about this file, drop everything, and go to the home page index.php.
index.php calls data from a database
Is this possible using a .htaccess file?
I have exactly the same problem with another of my sites.
Any help would be greatly appreciated.
Cheers, Al.
I dont think you can directly redirect an error document but you can catch nonexistent files and folders
!-f means not a file !-d means not a directory, $1 is whatever is in (.*) (the path in the url)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?errorurl=$1 [R=301,L]
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php

.htaccess rule stacking and 404 redirect

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]

vanilla 2 broken links

I have a vanilla 2.0.18b2 installation on my webserver in a subfolder. Forum works, but I can't sign in. When I click a link "sign in" the page goes blank. I think that this is a problem with .htaccess file cause I had a working solution but I wanted to change something and broken the file.
Another thing is that, when I am typing forum.mysite.com it says:
Fatal error: Class 'Gdn' not found in /bootstrap.php on line 47
when I type mysite.com/forum it view the website but with no styling, and when I click a link it goes 500 Internal Error.
Only when I type mysite.com/forum/index.php it looks normal.
My .htaccess file looks like that:
# Modified
# If you modify this file then change the above line to: # Modified
<IfModule mod_rewrite.c>
RewriteEngine On
# Certain hosts may require the following line.
# If vanilla is in a subfolder then you need to specify it after the /.
# (ex. You put Vanilla in /forum so change the next line to: RewriteBase /forum)
RewriteBase /forum/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php\?p=$1 [QSA,L]
#RewriteRule ^$ index.php/$1
</IfModule>
Try clearing the cache by deleting all the files and folders inside the cache folder.
Was facing the exact same problem and clearing the cache solved the issue!
Try changing the RewriteRule to this
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]

Resources