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

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.

Related

How to use apache to reroute files to other directory?

I am maintaining a website someone else wrote. He used absolute path in the html to request assets files, and he assumed the project is always installed at the root of the domain.
Now I am testing the website; I installed the project at a subdirectory. And I want to redirect files that start with a certain characters to a subfolder.
For example, he wrote something like this
<img src="/en/wp-content/themes/.....">
So now the server will start searching files at the root like example.com/en/wp-content/....
I installed the project at /foo/en, example.com/foo/en. I want to use .htaccess to reroute urls that start with /en/wp-content/themes to /foo/en/wp-content/themes. What should I do?
This is how I wrote it:
RewriteEngine On
RewriteRule ^en/wp-content/themes/(.*)$ /foo/en/wp-content/themes/$1
But it doesn't work....
I would change the line only a little bit just to be sure that this is not interfering with any other htaccess files use [L] or [END]:
RewriteEngine On
RewriteRule ^/?en/wp-content/themes/(.*)$ /foo/en/wp-content/themes/$1 [L]
[L] means Last https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l
[END] https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_end

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

read images from root directory in subdomain with htaccess

I have a domain like example.com where root directory is web.
I have created a subdomain happy.example.com where directory is outside web folder called happy and connected it to happy.example.com.
My webpage tree looks like
happy
web/images
So all my images for root directory are stored in (web/images)
example.com/images
So a full path to an image can be
example.com/images/me.png
Now i have created a sudbdomain which i call:
happy.example.com
What i would like to do is if i type
happy.example.com/images/me.png
then i should be able to see the picture.
So somehow i need to link all images folder to a subdomain from root directory in web.
I hope you guys got my question.
I guess i shoud have an htaccess file with all funny stuff in happy folder?
Cheerz
Since the two document roots of your two domains aren't connect to each other (one inside the other, or the same), you'll need to either redirect or proxy, or, use a script.
To redirect is easiest, but it'll change the URL in the browser's location bar:
Redirect 301 /images http://example.com/images
or using mod_rewrite:
RewriteEngine On
RewriteRule ^images/(.*)$ http://example.com/images/$1 [L,R=301]
To proxy, you need to have mod_proxy loaded, which isn't always the case if you're using a webhost or hosting service. But you can use the P flag in mod_rewrite to do this:
RewriteEngine On
RewriteRule ^images/(.*)$ http://example.com/images/$1 [L,P]
The last option is to route all image request to a script, like a php script or something. And the script itself returns the image:
RewriteEngine On
RewriteRule ^images/(.*)$ /load_image.php?image=$1 [L]
then you'd have something like:
<?php
header("Content-type: image/jpeg");
readfile('../web/images/' . $_GET['image']);
?>
Obviously, you'd need to check the extension and return the correct content type depending on the image type.

How manage several folders in my domain?

I want create subdomains like this:
domain.com/type/city
An examples:
domain.com/restaurants/new_york
domain.com/hotels/new_york
domain.com/restaurants/chicago
I have thousand of cities in a mysql database.
I thinked in some options:
Thousand of folders with an index.php for redirect (I think wrong way).
Create an sitemap with all links (domain.com?type=hotels&city=chicago) and manage they by code with the database.
Apache?
Please, which will be the best way for this? Thanks in advance!
You can solve this with a combination of PHP and Apache configuration. That is the most common solution and seen in popular PHP website software such as Drupal and Wordpress.
The idea is to let Apache send all traffic to one index.php file and pass the rest of the path as a parameter for PHP to handle with it.
You will need to be carefull with a few edgecases though; if file such as ./public/styles.css is requested, you don't want to serve that trough your PHP application but want apache to serve the file directly. Existing files will need to be handled by apache, all else by you application.
In your .htaccess:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
The first line tells Apache to send normal files by itlself. Second line does the same for existing directories. Third line avoids that browsers (most notably version IE6) who request the example.com/favicon.ico don't hammer your PHP application.
Then it passes everything along to index.php and adds the rest of the path into the q param.
Inside index.php you can then read that, and take action with that:
<?php
$path = $_GET['q'];
$params = explode('/', $path);
print $path;
print_r($params);
?>
Thousands of folders would be the wrong way that is for sure.
If you start creating the sitemap with links of the type domain.com/?type=hotels&city=chicago you get a nice structure that you can manage programatically.
First get this started and working, then look up .htaccess and mod_rewrite which you can then use to map from domain.com/type/city to your links already functioning.
This seems both to be a good strategy for getting something working fast, and for ending up with the prettiest solution.

Changing a file's URL without physically moving it

I have a site, running Linux + Apache.
I have a file in my root directory, let's say file.php.
I want the URL to the file to be "domain.com/newdir/file.php", but I don't want to actually create the newdir and move the file there because it would be a huge hassle to update many many links all over my site.
Is there a way to accomplish this, meaning making the file accessible by the new URL without moving it?
Thank you.
On this site: workwith.me, you can find information about .htaccess and mod_rewrite. For your example you have to make a file called .htaccess and put it in the root directory. The file should contain these directives:
RewriteEngine on
RewriteRule ^newdir/file.php$ /file.php [L]
You can do this for every file you want to rename.
Four possible solutions I can think of:
If your OS supports it, create a symlink:
mkdir /home/foo/htdocs/newdir
ln -s /home/foo/htdocs/file.php home/foo/htdocs/newdir/file.php
... and make sure Apache is configured to follow them:
Options FollowSymLinks
Create an Alias or AliasMatch (probably overkill)
Good old mod_rewrite:
RewriteEngine One
RewriteRule ^newdir/file\.php$ file.php [L]
Ugly: use a custom 404 error page with a PHP script that checks $_SERVER['REQUEST_URI'].
I guess the standard solutions are #1 and #3.