htaccess is not ignoring images like it should? - .htaccess

I have an htaccess file that is supposed to be redirecting any non-existing files/folders/etc into the application's index.php script where they are handled by the application's SEO rewriting.
The problem is that any missing images are also redirecting into the application which is causing a lot of additional load and is unnecessary. I've been attempting to get this htaccess file to ignore images, but even though this should be working, it's not, and I'm completely out of ideas as to why...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !.*\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]
When I make a request to http://www.domain.com/folder_that_doesnt_exist/image.jpg this redirects to index.php
When I make a request to http://www.domain.com/folder_that_does_exist/image.jpg it also redirects to the index script
I'm not sure what I'm missing here because unless OR is specified, shouldn't the RewriteRule only be applied if the request passes all of the RewriteCond statements? which it clearly should not be passing...
Update:
I've modified the code to the following just to eliminate possible issues, but it still redirects all non-existing images into the script...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]

Maby try this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteRule .* - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

Related

Htaccess - manage catch all in multiple directories and one single htaccess file

I'm trying to dispatch all traffic in multiple directories based on a specific keyword.
I have the following directory structure:
dotcom/
dotcom/directory1/ (with subdirs)
dotcom/directory2/ (with subdirs)
dotcom/directory3/ (with subdirs)
I have a .htaccess file located in dotcom and I would like to redirect everything behind each directory to an index file in each directory.
Example:
dotcom/directory1/anything/blabla to dotcom/directory1/index.php
dotcom/directory2/anything/blabla to dotcom/directory2/index.php
dotcom/anythingNotExisting to dotcom/index.php
Anything not in one of the existing directories should be redirected to dotcom/index.php
I tried the following for dotcom:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This catches everything
But when I tried to add conditions like the following, I get a 404:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/directory1/(.*)$ directory1/index.php?path=$1 [NC,L,QSA]
With this, if I try to access dotcom/directory1/blabla I have a 404 while if I access dotcom/directory1/ it goes to the right index.php
I have tried to use the full path dotcom/directory1/ but it doesn't help.
You may use these rules inside dotcom/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# ignore all rules below this for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# for URIs starting with know directory paths
RewriteRule ^(directory1|directory2)/(.*)$ $1/index.php?path=$2 [NC,L,QSA]
# everything else
RewriteRule .+ index.php?path=$0 [L,QSA]
I have found something that works with the following:
RewriteEngine ON
RewriteCond HTTPS off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory1/(.*)$ /dotcom/directory1/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory2/(.*)$ /dotcom/directory2/index.php?path=$1 [NC,L,QSA]
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dotcom/index.php?path=$1 [NC,L,QSA]
This way I'm catching everything from /directoryX/ and redirect it to the root of the directory, everything else go to dotcom

.htaccess Rewrite QSA

So I have an htaccess file that I'm working on for someone, and I'm stuck on passing the URL arguments. Basically any and all arguments need to be passed, however I don't know all the queries, which is where I get lost and need help.
I've literally spent the past hour and a half digging through the internet and read about QSA's but have just gotten completely lost... Needless to say, I'm not well with htaccess files. Lines 1-23 are for something else related, but with the pages themselves.
Here's a few examples in case you're confused:
"/events/?query=dark needs to execute events.php?query=dark"
"http://mywebsite.com/authorize/?name=john would actually execute http://mywebsite.com/authorize.php?name=john"
Here's what I have so far, line 25 and down is just an official query I have atm.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ "domain"/$1 [R=301,L]
ErrorDocument 404 /404.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /events/?$ [NC]
RewriteRule .* /events.php [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /authentication/?$ [NC]
RewriteRule .* /authentication.php [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /databases/?$ [NC]
RewriteRule .* /databases.php [R=301,NC,L]
RewriteCond %{QUERY_STRING} name=(.*)
RewriteRule ^authorize/(.*) /authorize.php?name=%1

Using an IF statement in a HTACCESS ReWrite rule

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
the above i have in my htacces file which rewrites directory/page to index.php?id=directory/page
thats working fine.
I also want to be able to add the following to it:
domain.com/sections/page rewrites to index.php?section=1&id=page
domain.com/sections/page2 rewrites to index.php?section=1&id=page2
domain.com/page rewrites to index.php?id=page
the ID is going to be different for each page
You have to take a look at RewriteCond and RewriteRule directives.
That's a sample .htaccess based on your edit.
RewriteEngine On
# This will process the /sections/(.*) requests, ?section=1 will be appended to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sections\/(.*)?$ index.php?section=1&id=$1 [L,QSA]
# This will process the other requests, as it does now.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]

htaccess displays error 500 when adding more than 3 rules

this is my htaccess:
Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home index.php
RewriteRule ^register register.php
RewriteRule ^rules rules.php [L]
When i add fourth rule to htaccess it returns 500 error.
so htaccess is allowing only 3 rewrite rules?
First thing is when you receive an error you should always check your Apache error_log file. It will give you the specific reason for the error. We would only be guessing at what your problem might be.
Now this might not be "the" issue however the RewriteCond only works for the RewriteRule directly following it. You can't use the same condtions for all of your rules. You should also get in to the habit of using the [L] flag on the rules to prevent it from reading the next rule once it has matched.
Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/?$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^register/?$ register.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^rules/?$ rules.php [L]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

Resources