allow a dir in .htaccess - apache

i have the following htaccess file for my codeigniter installation.
Everything works, except that it blocks my xml files and urllist for google sitemap submission.
How would I allow the whole dir /xml or maybe .xml filetype to be added.
I have absolutely no idea how .htaccess files work...
Thanks!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php

Below is my .htaccess file. I think the whole key is the RewriteCond (re-write condition).
You can simply put the file extensions or folder names that you want to be ignored.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond $1 !^(index\.php|images|css|js|xml|uploads|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
#Set Charset to UTF-8
AddDefaultCharset UTF-8
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>

Related

htaccess - subfolder as DocumentRoot for CSS files?

How do I use the .htaccess file to set a custom DocumentRoot for CSS files? All CSS files are within a folder named "assets", and I'd like to omit the "assets" folder when loading in the CSS files for an HTML page.
This is the current code I am using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !assets/
RewriteRule (.*) /assets/$1 [L]
This code makes links relative to the assets folder, but it doesn't apply to CSS files for some reason, as I still need to use href="assets/styles.css" in order to load a CSS file from /assets. I'd like to simply use href="styles.css".
I would create only one .htaccess file to solve all your problems. Please put this into your /root folder and delete the other .htaccess files.
# This first part should be done by the webserver,
# if not than thing about to change you hoster but I put it here:
# Preventing direct access to any .ht file (.htaccess, .htpasswd, etc.)
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
Options +FollowSymlinks
Options -Indexes
# Start to Rewrite
RewriteEngine On
# For all URL starting with /css, /fonts, /img or /js
RewriteCond %{REQUEST_URI} ^/?(css|fonts|img|js)(/.*)?$ [NC]
RewriteRule ^.*$ /site/public/%1%2 [L]
# Redirect all to the Application if not done already
RewriteCond %{REQUEST_URI} !^/?site/public/index\.php [NC]
# but not if the URL starts with css, fonts, img or js
RewriteCond %{REQUEST_URI} !^/?(css|fonts|img|js)(/.*)?$ [NC]
# or if request is a real file
RewriteCond %{REQUEST_FILENAME} !-f
# or if request is a real directory but not the root directory
RewriteCond %{REQUEST_URI} ^/?$ [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the rest to the index.php file in your public folder
RewriteRule ^.*$ /site/public/index.php [NC,L]
It worked... I just refreshed my browser cache, and the CSS files was included properly.

Why is this .htaccess file routing static files to our front controller?

We have been using the following .htaccess file for years. We are using Apache 2.4.7. And for some reason, static files seem to be hitting our front controller index.php!
For example: https://example.com/apple-touch-icon.png is being handled by our front controller as we can see the response header includes X-Powered-By:PHP/5.5.9-1ubuntu4.3.
Can anyone spot the issue?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Force SSL
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
It appears that your ErrorDocument directive is causing this (loading index.php when some resource i.e. image, js or css file is not found.
You can comment out this directive to avoid this behavior:
ErrorDocument 404 /index.php

Make php files under a certain directory unaccessible using htaccess

I have a .htaccess file in place to redirect all requests that does not hit an existing file or directory to be parsed by my index.php file like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Our app bootstrap file is index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
This is working fine, But I'd like also to add another rule to prevent any direct access to php files under a certain directory, routing those calls to the same index.php file.
Like so:
request for example.com/something.php -> ok
request for example.com/themes/(anythinggoeshere)/somefile.php -> not
ok, route trough index.php
request for example.com/themes/(anythinggoeshere)/banner.png -> ok
I'm looking for a way to make those "rules" work to everything under the "themes" folder without breaking my current ones.
Thanks!
You can have a new rule to handle those .php requests:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# handle .php requests via index.php
RewriteCond %{REQUEST_URI} !^/themes/ [NC]
RewriteRule ^[^/]+/.+?\.php$ index.php?/$1 [L,NC]
# Our app bootstrap file is index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Hide a folder from the URI with RewriteRule in a .htaccess file

I would really appreciate if you could help me building my .htaccess file.
What I want to achieve here is moving all of my domain's files and folders to a root subfolder ("mydomain") and make sure that the URL visible to my visitors still looks like
domain.com/file.extention
instead of
domain.com/mydomain/file.extention
or
domain.com/subfolder/file.extention
instead of
domain.com/mydomain/subfolder/file.extention
This is my folders tree:
/
/mydomain
/subfolder
index.php
test.php
and here is my current .htaccess file:
<IfModule mod_rewrite.c>
# System symbolic links are allowed.
Options +FollowSymlinks
# Runtime rewriting engine enabled.
RewriteEngine On
#
# BEGIN DOMAIN
#
# Make 'mydomain' subfolder the root folder for the domain.
RewriteCond %{HTTP_HOST} ^((www\.)?domain\.com) [NC]
RewriteRule ^\/?$ mydomain/ [NC,L,S=1]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?mydomain/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ mydomain/$1 [NC,L]
#
# END DOMAIN
#
</IfModule>
The problem here is that it works when I browse for
domain.com/subfolder/
but not with
domain.com/subfolder
and with
domain.com/index.php
but not with
domain.com/test.php
Thank you all,
Matteo
Changing domain.com/mydomain/file.extention to domain.com/file.extention
use this for example
RewriteRule ^mydomain/(.+)$ http://www.domain.com/ [R=301,L]
But if you redirects working just for some pages check that you clear browser cache or use another one instead

Mod_Rewrite rewriting directory I dont want it to

So I am using Kohana which is useful if you know it, but not needed to assist me.
I have the following mod_rewrite rules:
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/store/.*$
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php [L]
So I am trying to rewrite all requests for files and directories that do not exist to index.php.
However, I want any request sent to mydomain.com/store/* to go through as there is another htaccess file in the store directory that does work there. That does not seem to be working at the moment. Any ideas?
Full htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
#ErrorDocument 404 http://www.mydomain.com/404Page.html
#Options +FollowSymlinks
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
RewriteCond %{REMOTE_ADDR} !^myip
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://www.mydomain.com/maintenance.html [R=307,L]
##301 Redirect Rules##
#some 301 redirects i did not include here
##Kohana Redirect Rules##
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system|kohana|vendors)\b.* http://www.mydomain.com/ [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?store/
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php [L]
Try this condition:
RewriteCond %{REQUEST_URI} !^/?store/
There is no need to check the characters after the directory. I made the first slash optional if I remeber correctly is the first slash only visible if your server configuration does not contain a tailing slash.
The issue turned out to be in the .htaccess file in the store directory not the one in the webroot. Thanks all, and sorry for my stupidity. IF anyone wants to leave comments on how to debug something of this nature for future users that would be awesome.