Access external files as local files with htaccess - apache

I have a source and several user domains. I'm keeping JS, CSS and image files on the source domain and in the user domains I'm including those stuffs like source.com/js/jquery.js but I want to show them like user1.com/js/jquery.js, user2.com/js/jquery.js without the files physically there.
source.com/css/bootstrap.css
source.com/js/jquery.js
source.com/user/1/library/picture.png
source.com/user/2/library/picture.png
user1.com/css/bootstrap.css
user1.com/js/jquery.js
user1.com/library/picture.png
user2.com/css/bootstrap.css
user2.com/js/jquery.js
user2.com/library/picture.png
Is this possible with htaccess? And for helping, all of these source.com, user1.com, user2.com are in the same server.
Thanks for any help.

Okay, test this :
in user1.com .htaccess use RewriteRule
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^js/jquery.js$ http://source.com/js/jquery.js [P]
</IfModule>
then your url is something like this http://user1.com/js/jquery.js and file will be read from http://source.com/js/jquery.js without changing url.
This worked for me.

Related

How to prevent user to access images in multiple directories via the url

I am using .htaccess to prevent user to access images through url.
Thanks to this question, I could prevent users to access .js files.
RewriteEngine ON
RewriteRule ^frontend/assets/(?:js) - [R=401,NC,L]
However, when I applied the same rules in order to denied access to dir1 and dir2, the access is only denied to images in directory dir1.
RewriteRule ^frontend/assets/images/(?:dir1|dir2) - [R=401,NC,L]
Is there a way to handle this and prevent users to access files in directory dir2 as well ?
Please try following htaccess rules file. Apart from fixing regex we need to be careful on where we need to place the rule. So place this rule before your previously used rule(s).
Please make sure to clear your browser cache before testing your URLs.
##Enabling RewriteEngine here...
RewriteEngine On
##Rewrite rule for images with checking users OR uploads here...
RewriteRule ^free/frontend/assets/images/(?:users|uploads) - [R=401,NC,L]
##Rewrite rule for images OR css OR js folders here....
RewriteRule ^frontend/assets/(?:images|css|js) - [R=401,NC,L]

How Remove a folder form url using .htacces

I develop a new site for a client and I don't want to move to the root folder of the server.
Besides that, the hosting service do not alow me to change the physical path.
So I decide to use .htaccess to handle this.
My .htaccess file is like this:
AddHandler php56-script .php
suPHP_ConfigPath /home/balletpaulacastro/
Options +FollowSymLinks
RewriteEngine on
So when the user try to access www.balletpaulacastro.com.br they will be redirected to www.balletpaulacastro.com.br/bpc where the new site is located.
What i want is to rewrite the url to not show de /bpc/ folder.
I try many examples, but i'm not a coder, so if you guys can write down the entire code with my actual url and folders, that will be so nice.
Thanks in advance
Raul
Try :
RewriteEngine On
RewriteRule ^((?!bpc).*)$ /bpc/$1 [NC,L,QSA]
This will rewrite
example.com
to
example.com/bpc

mod_rewrite inserting full path to file

I need to create a rewrite to take traffic going to mp3/mp4 files in a specific subdirectory and then route them to a PHP file that tracks download stats etc before routing them to the actual file location since iTunes requires your podcast RSS contain actual media file extensions (.mp3, .mp4, etc)
I have created rewrites before with no problem but now I am running into an odd issue on this company's server.
My .htaccess located at www.company.com/companytools/podcasts
RewriteEngine on
RewriteRule ^/(.*).mp3$ /test.php?file=$1 [r=301,L]
Right now it is partially working it does act upon the mp3 file but ends up including the full path to test.php after the domain, so I end up with a 404 page looking for this URL:
www.company.com/www/internal/docs/companytools/podcasts/test.php?file=test
basically I need the path, but only the /companytools/podcasts part.
Any help is appreciated.
You may not need R=301 here to hide actual PHP handler.
Try this rule with RewriteBase:
RewriteEngine on
RewriteBase /companytools/podcasts/
RewriteRule ^(.+?)\.mp3$ test.php?file=$1 [L,QSA]

Using .htaccess to restrict access to files

I have 2 domains hosted on the same account but I want to restrict the second one to not access files.
I have abc123.com and zyx987.com.
In my php I do everything I need to display the domain name and template based on the domain but I want only the /download files to be accessed from abc123.com.
The download folder has files like: file1.pdf or file2.zip.
I tried the 'deny from all' but this didnt work since the other domain is blocked too.
Is there another way?
Here's what you have to do. Put this in your .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?zyx987.com$
RewriteRule ^download - [F]
This will block any request like:
/download/
/download/file1.jpg
/download/another/file.zip
and give a permission denied.

Using .htaccess mod_rewrite to bypass a deep directory structure to CSS assets

I have a setup like so
http://localhost/
http://localhost/ci_tada/
http://localhost/ci_tada/application
http://localhost/ci_tada/application/views
http://localhost/ci_tada/application/views/css
http://localhost/ci_tada/application/views/css/master.css
I dont want to have to write
http://localhost/ci_tada/application/views/css/
every time i wish to access a css file (the same will apply for images ect)
I want to be able to just use
http://localhost/ci_tada/css/master.css
and have it load the correct file.
The .htaccess file is located in the ci_tada folder.
Looks like you're using some sort of routes within a framework. You should check, because some frameworks give you the option to do it in the app configuration.
<IfModule mod_rewrite.c>
RewriteEngine On
# directory base, untogle if you want to
# rewrite only starting from /<directory/path>
# RewriteBase /
RewriteRule ^/css/(.*)$ index.php/ci_tada/application/views/css/$1 [PT,L]
#or RewriteRule ^/(.*)/css(.*)$ index.php/$0/views/css$1 [PT,L]
#or RewriteRule ^/(.*)/css/(.*)$ index.php/$0/views/css/$1 [PT,L]
</IfModule>
Beware that the last two redirect everything that contains /css/ in the path.
edit:1: It is considered best practice in CI (from what I've read), to set a static directory on your root like this:
/
.../static
......./css
......./js
.../application
......./controller
.../...
So that you can simply use /static/css/file.css inline in your views. Also see these resources if they can help:
http://codeigniter.com/forums/viewthread/60563/#297784
I feel you are talking about Zend Framework. Your few lines are
/
/application
How can every request can be redirected to CSS/? Then your application will not work!
In Zend Framework, application/layouts/scripts/layout.phtml, you mention css file like this:
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>
Css file go in project folder. It is outside application folder:
\quickstart2\public\css.
Hope I will not receive -ve answer.