Redirect images in .htaccess - apache

I have two domains pointing to one application with different directories that is frontend on www.frontend.com and backend on www.backend.com. I have placed all images in the frontend/uploads folder, while on backend I cannot access the images on frontend/uploads.
How can I redirect all ^/uploads to www.frontend.com/uploads using htaccess?
I have tried this:
RewriteCond %{REQUEST_URI} ^/uploads/(.*)$
RewriteRule ^uploads/(.*)$ http://frontend.com/uploads/$1

Personally I'd just use a filesystem level symbolic link, as far more efficient (Unix, Linux, BSD, Darwin {OS X}) eg.
ln -s /srv/www/frontEnd/htdocs/uploads /srv/www/backEnd/htdocs/uploads
and make sure you have the FollowSymLinks option set in the backend Directory block eg.
<Directory /srv/www/backEnd/htdocs/uploads>
Options Indexes FollowSymLinks
</Directory>
But assuming the www.frontend.com site is accessible to your www.backend.com users all you need in your backend config is:
RewriteRule ^/?uploads/.* http://frontend.com%{REQUEST_URI} [NC,L,R=301]
If you take this approach I'd stick the rule in the httpd.conf, rather than a .htaccess, as that file is only parsed once on server startup, and the rule compiled, rather than having to parse the file for every request.

Related

mod_rewrite doesnt work if file with same name as argument exists

I'm experiencing some issues with mod_rewrite in htaccess.
Let's for instance say I request example.com/foo/, it works perfectly if I don't have a file starting with "foo.*" in the root directory.
Let's say I have news.php, sitemape.xml, style.css in root, I can't use /news/ or /sitemap/ or /style/ , it will give a 404 like /news.php/ etc.
Here's my rewrite string. It works locally with my Apache 2.2.22 but not at my web-host with the same Apache version.
RewriteRule ^([A-Za-z0-9]+)/?$ index.php?category=$1 [NC,L]
Anyone has a clue?
This sounds like Multiviews rearing its ugly head when its not wanted. It may be that your host automatically turns on the Multiviews option by default, and mod_negotiation then tries to "guess" what the request is for, and if it's close enough (like with /news/ and /news.php), it will automatically serve it, and disregard whatever mod_rewrite rules you may have.
Try turning off multiviews. You can do this in your htaccess file using the Options directive (assuming your host has allowed Options):
Options -Multiviews

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.

How to prevent Apache / mod_rewrite from treating path as file with same name

I'm using WAMP Server, mostly configured as-is out of the box. I'm having trouble getting mod_rewrite to behave as expected locally (everything works fine on a production server).
I have a PHP file located at:
/ajax/graphs/get-graph.php
The way this file is normally invoked is via a bootstrap file loaded by
/index.php
I have a .htaccess file at the root with the following rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
So, basically, when my app requests via AJAX a call to /ajax/graphs/get-graph/it should be directed to /index.php.
The problem is, Apache/mod_rewrite sees the request path and loads /ajax/graphs/get-graph.php directly.
How do I prevent Apache from assuming that /ajax/graphs/get-graph/ is a valid file because a php file of the same name exists at that location?
It sounds like you've fallen into the trap of content negotiation ;-) As explained in the Apache documentation, there is an option called MultiViews which, when enabled, causes Apache to basically convert nonexistent directory names into their corresponding filenames.
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files...
The intent is that you can have several versions of a file in different formats or languages, like
/some/dir
- foo.en.gif
- foo.en.png
- foo.en.jpg
- foo.fr.gif
- foo.fr.png
- foo.fr.jpg
and Apache will choose the best one based on the preferences provided by the browser.
To fix it, all you should need to do is add the directive
Options -MultiViews
in a <Directory> or <Location> block corresponding to /ajax/graphs. Or, if you don't have access to the main server configuration, you can put it in /ajax/graphs/.htaccess.

Apache Mod_ReWrite Suddenly Stopped Working

I had mod_rewrite set on my server to rewrite a url like the following
http://www.example.com/1
to
http://www.example.com/index.php?show=1
In order words a URL shortern. Everything was working fine when the system was running under a sub-domain on my development site, but now it just generates a Not Found error, although if I manually enter the url /index.php?show=1 it works fine.
So the only changes is the urls switching from
http://www.site.example.com
to
http://www.site.com
however it's still running on the same server and the same sub-folder inside public_html on the server just the new domain name has been pointed to that folder.
The folder it's stored in is /public_html/paste
The full .htaccess file running in the directory is
# Set Default File
DirectoryIndex index.php
# Turn ReWrite Engine On
RewriteEngine on
# Create Rule To Write URLs To Shorter Versions
RewriteRule /([a-z0-9]+) /index.php?show=$1
I can't enable RewriteLog as the hosting doesn't allow it for some reason.
It sounds like the AllowOverride directive is not properly set for that folder. In your Apache configuration, you should make sure that the Directory or Vhost you're using for the primary domain has the AllowOverride set to All
http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride
You probably need to specify the RewriteBase directive.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
I'll also note that Options +FollowSymlinks would be good to have in there too in case you ever turn it off further up the config chain (rewrite wont work without it).

How to Route Various domain Aliases to Fetch their Own Folders with Own Webpage PHP Files?

imagine the root of a server where multiple aliases such as website.nl; website.de; etc. all direct to the root the root \httpdocs\ with this physical hosting:
httpdocs\...
httpdocs\holland\ # webpages in Dutch (home.php | contact.php | etc)
httpdocs\deutsch\ # webpages in English (home.php | contact.php | etc)
httpdocs\images\ # all multilingual webpages share the same images
httpdocs\js_css\ # all multilingual webpages share the same scripts/layout
httpdocs\.htaccess # here be a clean root, nothing else than .htaccess
Thus, only the webpages .php differ: the rest they all share the same! Now imagine that you want to configure .htacces via apache script to make "bridge the gap" if you will, between the root and the folder, making it possible to type this in browser and below water fetching the right php webpage, but keeping the shorter url in the browser:
website.nl/home.php
//files fetched should come from the holland folder associated with website.nl
website.de/home.php
//files fetched should come from the deutsch folder associated with website.de
(As opposed to seeing this in the browser: website.nl/holland/home.php | website.de/deutsch/home.php)
What apache script line will do such thing?
Thanks: Much appreciated!
You need to have "mod_rewrite" installed in apache.
Assuming you do, place a .htaccess file in the root directory of that account
(probably:)
~/public_html/.htaccess
and then put this in that file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.nl(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^www.website.nl(.*)$
RewriteRule ^.*$ "http\:\/\/www\.website\.nl\/holland\/$1" [L,P]
And then repeat the rewrite condition for website.de
Basically that says, grab the section after the website.nl part, and paste it on the part after holland, but the "L,P" says to make that a silent redirect and keep the user on the same url as they entered with.
goodluck!
Assuming you have full control over the server configuration, I would tend to a setup without mod_rewrite. Instead I'd use two separate VirtualHosts for each country site, and Alias directives to map the shared directories. In an abridged version, something like this:
<VirtualHost website.nl>
DocumentRoot /httpdocs/holland
ServerName website.nl
Alias /images /httpdocs/images
Alias /js_css /httpdocs/js_css
</VirtualHost>
<VirtualHost website.de>
DocumentRoot /httpdocs/deutsch
ServerName website.de
Alias /images /httpdocs/images
Alias /js_css /httpdocs/js_css
</VirtualHost>
this lets you work freely in both languages, and add new resources and URLs without having to edit anything - only if a shared resource is added, you have to touch the Alias directives and restart the server.