htaccess - subfolder as DocumentRoot for CSS files? - apache

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.

Related

Remove index.php from URL of Laravel

I am using Laravel 5.5.12 in Linux Mint.I am using LAMP stack. I would like to remove index.php from URL. My mod_rewrite apache module enabled.
My .htaccess file located in public folder and it contains following code.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
I renamed the server.php in the Laravel root folder to index.php and copy the .htaccess file from /public directory to Laravel root folder. But it is not working.
I placed below code in .htaccess file
<IfModule mod_rewrite.c>
# Turn Off mod_dir Redirect For Existing Directories
DirectorySlash Off
# Rewrite For Public Folder
RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But this not working.
Could anyone help me in this regard ?
Reset anything you have changed back to the default.
In your apache virtual host configuration, ensure you have the DocumentRoot correct (it will be something like /var/www/html/laravelproject/public).
You should not need to make any changes to the .htaccess file in the public folder as this handles rewriting the url to remove index.php. However, in some environments, I have had to add
RewriteBase /laravelproject
to the .htaccess in the public folder.
I found the answer here:
https://ma.ttias.be/remove-index-php-from-the-url-in-laravel/
To summarise, I added the following to the .htaccess file, directly below the existing index.php entry:
RewriteRule ^index.php/(.+) /$1 [R=301,L]
Go to httpd.conf file, search for the line DocumentRoot and change it to where ever your index.php is. For example:
DocumentRoot "C:/xampp/htdocs/myappfolder/public"

.htaccess redirecting sub domains to sub domains name folder

My site structure is as follows:
/
static/
www/
index.html
img/
www2/
index.html
img/
www3/
index.html
img/
I have this .htaccess that does the following:
According to a subdomain for instance www.domain.com/index.html i redirect to the right subdomain folder which is /www/index.html.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/*
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
</IfModule>
It works perfectly when i am accessing the site with www.domain.com/index.html
but not without index.html meaning
www.domain.com/ .
I have tried many condition but non of them seems to work , and if it works it breaks what i have now.
Which condition should i add for redirecting the www.domain.com/ to /www/index.html ?
Thanks
EDIT
The error i am receiving is:
"Forbidden You don't have permission to access / on this server."
meaning i am redirected to the / folder which doesnt contains an index.html.
To me it sounds like you don't have a directory index set. There is nothing wrong with going directly to the file but apache doesn't consider index.html your index file. And with directory listing turned off you will get that error. You don't need rewrite for that. Use the directive. That should prevent the 403 Forbidden.
DirectoryIndex index.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/*
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
</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.

allow a dir in .htaccess

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>