How to use apache to reroute files to other directory? - apache

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

Related

htaccess RewriteRule problems

I have a web page which works fine on live server. However some links to files (jpg, pdf and others) which are created with cms editor contain relative paths.
When I run that page on my local test server which serves the pages out of a sub folder of localhost the relative paths to the files are wrong since they are missing the subfolder at the beginning. The html page loads fine. It's just some files in it that have wrong path and won't load.
page loads from http://localhost/level1/
files are trying to load from http://localhost/level2/ and I get 404s.
They should be loading from http://localhost/level1/level2/
So I setup a RewriteRule to correct the path but no matter what I have tried I can't get it to work. I have tried various flags including [R,L] but nothing changes the URI in the html.
currently I have:
RewriteRule ^/level2/(.*)$ /level1/level2/$1 [R]
Any suggestions?
Thanks
Sounds like those links are not relative paths but absolute ones (starting with a leading slash (/). That is why the issue occurs at all. Relative paths make much more sense.
This would be the version to be used inside your http servers host configuration:
RewriteEngine on
RewriteRule ^/level2/(.*)$ /level1/level2/$1 [L,QSA]
Here the version for .htaccess style files (note the missing leading slash):
RewriteEngine on
RewriteRule ^level2/(.*)$ /level1/level2/$1 [L,QSA]
You could use a version that can be used in both situations:
RewriteEngine on
RewriteRule ^/?level2/(.*)$ /level1/level2/$1 [L,QSA]
Note however that in general one should always prefer to place such rules inside the http servers host configurations. .htaccess style files are notoriously error prone, hard to debug and they really slow down the server, often for nothing. .htaccess style files only offer a last option for those who are using a really cheap web hosting provider. Or for situations where a web application has to write its own rewrite rules, which obviously is a security nightmare on its own...

Use .htaccess to transparently manage versions of site depending on extension

After reading and trying a lot of tutorials and howtos... I have not found a way to do this using .htaccess ;-P
I have this folder structure:
somepath/mywebPHP_v314/ (incluing many subfolders with php files)
somepath/mywebJS_v007/ (incluing many subfolders with js files)
somepath/mywebCSS_v876/ (incluing many subfolders with css files)
somepath/mywebJPG_v543/ (incluing many subfolders with jpg files)
somepath/standardhostingfolder/.htaccess (Only file at this folder. No subfolders)
I would like to program .htaccess to get this behaviour:
Depending on the extension of the file, Apache will serve transparently the corresponding file.
E.g. when visiting www.mywebname.com/products/family/somearchive.php Apache will serve transparently somepath/mywebPHP_v314/products/family/somearchive.php (instead of serving the usual somepath/standardhostingfolder/products/family/somearchive.php)
e.g. when visiting www.mywebname.com/products/family/foto3.jpg Apache will serve transparently somepath/mywebJPG_v543/products/family/foto3.jpg
Thanks in advance!
P.S. Just for clarifying: "somepath/standardhostingfolder/" is the server's "web root" folder: If I delete somepath/standardhostingfolder/.htaccess: When visiting www.mywebname.com/products/family/foto3.jpg Apache will try to serve naturally somepath/standardhostingfolder/products/family/foto3.jpg .
This should work for .jpg, you can create 3 other rules accordingly:
RewriteCond %{REQUEST_URI} \.jpg$
RewriteCond %{REQUEST_URI} !^/mywebJPG_v543
RewriteRule (.*) /mywebJPG_v543%{REQUEST_URI} [L]

.htaccess rewrite root to folder, yet block direct urls to same folder

I've been researching and trying for a week to accomplish this, but I haven't been able to find my solution. I'm sure it's possible, but I'm just not an expert in the depths of voodoo magic.
The setup:
An installation of MantisBT located in
mysite.com/mantisbt/currentver/mantis-1.3.19
When I perform an upgrade, I want to archive all old versions and old
database dumps to /mantisbt/oldversions/ to keep things tidy.
I also have other utilities and pages in various subdirectories, for
instance "mysite.com/utils"
The goal:
I want users to type in mysite.com/ (root) and have the URL rewritten
(transparently) to /mantisbt/currentver/mantis-1.3.19/ so that they
don't see my directory structure and it looks like the mantisbt app is
located in the root directory.
I also want protection from anyone trying to directly access a
subdirectory beginning with "/mantis". For instance, if
someone directly types mysite.com/mantisbt/currentver/mantis-1.3.19/
into their browser, I want them redirected back to the root directory
so they access the site from root just like everyone else.
I also need to allow my other subdirectories like mysite.com/utils to
be accessible if I type in the full path to them.
The biggest problem I've encountered is that Apache loops through your .htaccess file again every time the URL changes. So I get stuck in these rewrite loops. I've tried looking at every possible tool that Apache offers, but I'm seriously outgunned here. I could provide examples of what I've tried, they're obviously not correct, but ask me and I can post them.
You'd be far better off altering the DocumentRoot in your httpd.conf, and mapping in the utils directory, via a Location directive. It will be far cleaner, and Apache won't have to do some trickery with every request.
Anyway something along the following lines should work.
# Stop direct access
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteRule /?mantisbt/currentver/mantis-1.3.19 / [NC,L,R=301]
# Internally Rewrite everything apart from /utils to /mantisbt/mantis-1.3.19/
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteCond %{REQUEST_URI} !/utils [NC]
RewriteRule .* /mantisbt/currentver/mantis-1.3.19%{REQUEST_URI} [L]

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 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.