Ruby on Rails - .htacces RewriteRule to change image path - ruby-on-rails-3

The situation
I use Carrierwave to upload images to the default upload/ directory. Works fine. When I want to view the images in the front end of my online web server, they don't show.
If I use my application in my local setup, the images show.
The only difference between my local and web server's setup is that on my server the application is run in subdirectory called "/app". Locally it's in the root directory.
What I have so far
I have this Redirect command in my .htaccess:
Redirect 301 /uploads/model/image/54/image.jpg /app/uploads/model/image/54/imgage.jpg
I want to formulate a general RewriteRule out of it so that all images which have the path "/uploads/model/image/" are found and redirected to "/app/uploads/model/image/".
How can I do that? I've tried several RewriteRules like:
RedirectMatch 301 /uploads/(.*) /app/uploads/
or:
Redirect 301 /uploads/model/image/(.*) /app/uploads/model/image//$1
or:
RewriteRule ^.*/app/uploads/(.*)$ http://%{HTTP_HOST}/uploads/$1 [L,R=301]
What I've read so far
I've read those entries on Stackoverflow:
How to change image path using htaccess?
Use Mod_Rewrite to Rewrite ROOT to Another Path
.htaccess rewrite to redirect root URL to subdirectory
I really have no idea why this happens would be very happy to get any hint, tip or link that could help me out of that.

You're close. Try:
RedirectMatch 301 ^/uploads/(.*)$ /app/uploads/$1
or
RewriteRule ^/?uploads/(.*)$ /app/uploads/$1 [L,R=301]

Related

apache2 .htaccess redirect subdirectory to domain without including path

I am attempting to redirect a subfolder on an apache2 server to the root of an entirely different domain, without including the path of the folder in the new domain.
So, in mysite.com/folder-1/folder-2 I have a .htaccess file like so:
RedirectMatch 301 / http://a-totally-different-site.com/
This sort of works, but includes the path in the redirect. So, when you visit mysite.com/folder-1/folder-2 it goes to a-totally-different-site.com/folder-1/folder-2
What I wish to happen, is for the subfolder to redirect to the root, so visiting mysite.com/folder-1/folder-2 will go to a-totally-different-site.com
Is this possible with .htaccess? I have tried a few different approaches I found, but none are working.
Simple rewrite rule should do:
RewriteEngine On
RewriteRule ^ http://a-totally-different-site.com [R=301]

301 apache redirect non existing path using .htaccess joomla

I want to redirect a path: /folder/filename.html to: /folder/newfolder.
My host says I must create a dummy file at /folder/filename.html or it won't redirect. I can't do this as by creating the /folder, that messes up Joomla which would normally create that part of the path dynamically. I'm not sure I believe my host, surely it's possible to redirect a path regardless whether there is a file there?
In your .htaccess after RewriteEngine On just add this line
RewriteRule ^/folder/filename.html /folder/newfolder/filename.html [R=301,L]

htaccess redirect stuck in loop

In my tool that I wrote, I want only avible index.php file which is in the 'Public' subfolder, my root directory structure (of my application) is following
uapi(root)
/Application
/Config
/Public
Index.php
/Storage
.htaccess
And I want all folders except Public to be redirected to /Public/Index.php, and it cant be domain based because this tool will be available to download and install on the users server.
Here is the .htaccess, but instead of redirecting it normally opens the directory
Redirect 301 /Storage/ http://localhost:800/web/uapi/Public/
Redirect 301 /Application/ http://localhost:800/web/uapi/Public/
Redirect 301 /Config/ http://localhost:800/web/uapi/Public/
Redirect directives only work with absolute pathnames. So if you are using them your .htaccess file should be in the root of the webserver and there should be full pathnames (/web/uapi/Storage, for example). In my opinion it is better to achieve the same using mod_rewrite.
RewriteEngine On
RewriteRule ^(?:Storage|Application|Config)/$ Public [L]

301 redirect not working

I made a subdomain, and I am trying to redirect a page from the original domain to the subdomain, with the same dir structure.
My original page is this:
http://www.comehike.com/outdoors/alaska_hiking.php
I put this rule into the .htaaccess file under comehike.com/outdoors/ directory:
RewriteRule ^outdoors/alaska_hiking.php http://hiking.comehike.com/outdoors/alaska_hiking.php [R,L]
But as you can see from visiting the original url, it doesn't redirect. Any idea why?
Thanks!
I use apache server.
That RewriteRule line works for me when I stick it in my .htaccess file, are you sure you have RewriteEngine On?

How do I redirect a subdirectory and all files in it to the root using htaccess?

I'm trying to redirect all requests for a subdirectory and any files in it to the root directory, using htaccess. Currently I have redirect 301 /directory/ / But this redirects to the root with the file name requested appended to it.. ie www.domain.com/directory/this.html redirects to www.domain.com/this.html I don't want it to request the file at all. Is there a way to do this?
Thank you
Karl
You will probably have to use mod_rewrite. Assuming you place the .htaccess in the root directory, something like this should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^name-of-subfolder/.*$ / [R=302]
</IfModule>
NB. I put 302, as it's always sensible to use for testing, 301 can make it a pain checking changes with your browser. Once it's working feel free to change to 302.