redirect single image from one directory to another - .htaccess

I have one website, say www.example.com. So when I access "http://www.example.com/program/resources/foo.gif" it serves me foo.gif image from /usr/share/roundcube/program/resources/foo.gif, I found that in access log.
So what I would like to do is, I would like to copy that image from /usr/share/roundcube/program/resources/ to my webroot /var/www/www.example.com/webroot/img/ and write rewrite rule so that when request comes for foo.gif, it should serve from /var/www/www.example.com/webroot/img not from /usr/share/roundcube/program/resources/.
I've tried this
RewriteEngine On
RewriteRule /program/resources/(.*) /img/$1 [L]
It's working fine. But what if I want to make a rule for single image that is foo.gif ?

You can make it more generic and simpler than that.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/override%{REQUEST_URI} -f
RewriteRule .* override/$0 [L,NS]
Now just put any file you don't want to come from RoundCube into the override folder, e.g. override/program/resources/foo.gif

Related

Rewrite rules for localhost AND live environment

I want to add rewriterules that works in local environment (localhost) and on my liveserver.
Why?
I don't want to change rules when I test my project locally and upload it to the liveserver.
Adding Rules
Here an example
(ANY-URL)index.php?page=somepage
change to
(ANY-URL)/somepage
So I used a rewritemod generator and pasted this into it:
index.php?page=somepage
The rewriterule I got, looks like this: (of course my .htacces starts with RewriteEngine On)
RewriteRule ^([^/]*)$ /?page=$1 [L]
When I try to get to (http:)
//localhost/myproject/development/index.php?page=login it sends me to the root directory of my local development envirment. But the URL in the adressline doesn't change.
Testing
Of course I tried some other Rules by pasting the whole URL into the generator just to test if the rewrite thing works.
Also here the URL doesn't change to short-url but the server cant find stylesheets and javascripts anymore. I got redirected to the index.php
Possible solutions?
Maybe it has something todo with that "RewriteBase"?
Do i have to set a basepath?
My .htacces is located here:
//localhost/myproject/development/.htaccess
Later I also want to change paths that look like this:
(ANY-URL)index.php?page=somepage&second=hello&third=world&fourth=cool
Also here a I'm looking for a solution that works on both environments.
Since the htaccess is located inside a sub-directory, use RewriteBase:
RewriteEngine On
RewriteBase /myproject/development/
Once that is done, you use the base element in your php/html files. This will resolve the addresses for the scripts/stylesheets in your pages.
<base href="/myproject/development/" />
Now, the rewrites in htaccess would be:
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]

.htaccess Redirect in case of 404

I have this line of .htaccess
This line is used to get images from another server.
RewriteRule ^resources/fabricantes(.*)$ http://mysecondserver.com/arq/pictures/fab$1
than, if I have the url: http://myserver.com/resources/fabricantes/fab_1.jpg
this image will be get from: http://mysecondserver.com/arq/pictures/fab/fab_1.jpg
The Problem:
In some cases, the image doesn't exists on mysecondserver.com, how can I redirect to a "image unavailable" image in this cases?
First think you need to understand that this rule can only work from mysecondserver.com host not from server.com.
On mysecondserver.com place this .htaccess in /arq/pictures/.htaccess:
RewriteEngine On
RewriteBase /arq/pictures/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ no-image.jpg [L]
On mysecondserver.com, you would have .htaccess checking for -f status of the REQUEST_FILENAME. It could send the user back to the first server, if necessary. The alternative is to explicitly list all known failures back on your first server, which is a lot of work for you (even if you have a complete list). There's no way for the first server to know if a file actually exists on the second server.

htaccess rewriterule for directory is changing the url in a undesireable way

EDIT
After a comment from Seth below, and heading to a helpful apache page here, I have found that VirtualHosts are the way to go for the following issue.
/edit
--ORIGINAL POST--
First, a little background on file setup. I am running a LAMP server that hosts multiple domains. I have staging and live sites on this server, under different directories under the web root.
examples
/webroot/live/site1/[public files]
/webroot/live/site2/[public files]
/webroot/stage/site1/[public files]
/webroot/stage/site2/[public files]
The domains for each of these go to the IP of the server, which points at the webroot directory. I have an .htaccess file there to load the appropriate content based on the http_host.
examples
RewriteCond %{HTTP_HOST} ^www.site1-live.com [NC]
RewriteRule ^(.*)$ /live/site1/$1 [PT,L,QSA]
RewriteCond %{HTTP_HOST} ^www.site1-stage.com [NC]
RewriteRule ^(.*)$ /stage/site1/$1 [PT,L,QSA]
These work great for hitting the home page and any of the internal pages, even with the specific pages being like site1-live.com/view/123. Each site's htaccess handles those.
My issue (sorry it took so long to get here):
When I head to any subdirectory within a site, like www.site1-live.com/rss, the content loads just fine, but the URL changes to something like the following
http://www.site1-live.com/live/site1/rss/
Essentially showing the path from the webroot to the files.
How can I avoid this? I obviously want the url to remain www.site1-live.com/rss. Do I need an htaccess file inside the rss directory to block this somehow?
Thanks in advance!
replace ^www with ^(.*)
then have the whole url in the second line www.yourdomain.com/live/...
Doug,
why do you need the QSA flag?
Anyway, what is happening to you is that mod_index (or whatever is serving you directories) is redirecting you www.site1-live.com/rss (without the ending /) to the equivalent URL with the ending /.
If you don't use mod_alias or something list that on the rewritten URLs, removing the PT should work as you expect.

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

.htaccess with public folder

I have a directory structure with the following on localhost:
http://localhost/testing/
A directory structure exists inside of testing as follows:
/testing/public
/testing/public/index.php
/testing/public/img
/testing/public/css
..etc for the js and swf directories
A .htaccess file is inside the testing folder and the contents are as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /testing/
RewriteRule ^public$ public/ [R,QSA]
RewriteRule ^public/$ public/index.php?state=public [L,QSA]
RewriteRule ^stackoverflow$ stackoverflow/ [R,QSA]
RewriteRule ^stackoverflow/$ public/index.php?state=stackoverflow[L,QSA]
I am using PHP and inside of the /testing/public/index.php file I wanted to test that the $_GET['state'] is indeed saving the variable.
When I try to test out:
http://localhost/testing/public
$_GET['state'] is not found at all BUT
http://localhost/testing/stackoverflow
does indeed echo out that $_GET['state'] equals 'stackoverflow'.
What am I missing here??? Why is it that I cannot get the state=public in the first link? Thanks for the help!
This works fine on my system, but I think you are getting into trouble by having a rewrite rule with the same name as an actual file system directory. The file-system will generally take precedence. So when you load up '/testing/public' it just loads /testing/public/index.php without running your rule at all.
Try changing the rule to this:
RewriteRule ^public_$ public_/ [R,QSA]
RewriteRule ^public_/$ public/index.php?state=public [L,QSA]
Navigate to 'testing/public_', if that prints out 'public' as expected then you will know that was your problem.

Resources