Ignore files in non-existent paths using .htaccess - apache

I am having a problem with rewriting.
Here is my .htaccess-file
RewriteEngine On
RewriteBase /
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* - [L]
#load existent utility files and don't rewrite them to index.php
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$
RewriteRule ^(.*) $1 [L]
#load other files if they exist
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
#load index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
The last part is working but I want to avoid that my index.php is loaded when the code requests a non-exisiting image (as: www.mysite.test/images/foobar.png)
The first RewriteCond-Line should check if the request matches a file of image, JavaScript, and the like and the second RewriteCond line should check if the file does NOT exist.
The third one should check if the folder exists.
If the match succeeds NOTHING should be loaded.
It works but only if the folder exists. If the folder if the image doesn't exist it doesn't work.

You have many redundant rules. You can simplify the to:
RewriteEngine On
RewriteBase /
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ [NC]
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Ok there was still an issue with existing files, as Member anubhava said.
My final solution is:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteBase /
#load files if they exist
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt|php)$ - [L]
#load index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
This solution includes php-files in the second rewrite-rule. Because I have some php-files that are actually css-files and loaded by the browser.

Related

I need to remove language code from URL using htaccess

I have two language /ru and /uk. /uk is the default.
I need to remove /uk from the URL.
For example:
www.example.com.ua/uk/category
to
www.example.com.ua/category
But any URL with /ru must not change. ie. www.example.com.ua/ru/category.
htaccess look like
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^init.php$ - [F,L,NC]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(favicon|apple-touch-icon-|homescreen-|firefox-icon-|coast-icon-|mstile-).*\.(png|ico)$ - [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [NC,or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
In order to remove the /uk path segment from the start of the URL-path then you would need to do something like the following at the top of your .htaccess file:
RewriteRule ^uk/(.*) /$1 [R=302,L]
This does assume that you have already modified all your internal links and canonical tags to remove the /uk path segment.
If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent) only once you have confirmed that everything works as intended, so as to avoid potential caching issues.

Apache .htaccess to redirect all URLs except for files and directories

I am developing a web site currently located at a subdomain. In the .htaccess file I have the following:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z_\-]+)/?$ /index.php?url=$1 [L,NC]
RewriteRule ^([a-zA-Z_\-]+)/(.+)/?$ /index.php?url=$1&$2 [L,NC]
When I try to load an existing css file e.g. http://subdomain/path/to/existing/file.css Apache still redirects it to index.php. Is it possible to redirect all URLs to index.php except for existing files and directories?
The problem with your code is that the last rule is not conditioned, as you can only have one RewriteRule per condition or set thereof.
There's more than likely a better way to do this (perhaps using the skip flag S), but you can repeat your conditions in the meantime.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z_-]+)/?$ index.php?url=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z_-]+)/(.+)/?$ index.php?url=$1&$2 [L,NC]
Also, and as you have specified the NC flag, you need not specify A-Z in the pattern. Also, no need to escape the hyphen -.
Update: You could also try setting an environment variable, per this answer:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# If the conditions above are true, then set a variable to say so
# and continue.
RewriteRule ^ - [E=TRUE:YES]
RewriteCond %{ENV:TRUE} = YES
RewriteRule ^([a-zA-Z_\-]+)/?$ /index.php?url=$1 [L,NC]
RewriteCond %{ENV:TRUE} = YES
RewriteRule ^([a-zA-Z_\-]+)/(.+)/?$ /index.php?url=$1&$2 [L,NC]

Redirect to a specific subfolder using htaccess

Here's my folder structure:
website.com
main
sub1
sub2
I would like to be able to access the subfolder sub1 via the URL website.com/sub1, which I managed to do so by adding the following in my .htaccess under website/ :
RewriteCond %{REQUEST_URI} /sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ main/sub1/index.php?view=$1 [QSA,L]
My problem is that although the index.php is accessed, it is accessed all the time, even for the .js and .css files. So I tried to add the following rule before (although I though I excluded the files via my !-f condition before):
RewriteCond %{REQUEST_URI} /sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ main/sub1/$1 [L,QSA]
But as you can guess, no luck, no actual files ever go into that second block even when I delete the other one.
Try:
RewriteCond %{REQUEST_URI} ^/sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/main/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /main/$1 [L]
then your old rules:
RewriteCond %{REQUEST_URI} /sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ main/sub1/index.php?view=$1 [QSA,L]

CodeIgniter htaccess subfolder problem

I want a second website in my domain inside the folder test
www.mydomian.com/test
Apache server running on linux.
But when I load it in my navigator styles, images, helpers can't be found.
My htaccess is like this:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /test/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Thank you in advance
I have tried what you said, placing the htaccess file you gave me in the test folder and the other one in the root directory, it didn't work.
Other options?
Try adding a .htaccess file into your test folder instead.
I use this .htaccess content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
In the folder test I would place another .htaccess, with almost the same content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/index.php?/$1 [L]

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1