apache2 .htaccess redirect subdirectory to domain without including path - apache

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]

Related

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 Multiple domains in one root running different phps

I am trying to set up an .HTACCESS so I can have multiple domains with the same root directory. I want the htaccess to redirect to a php file dependant on domain name. I don't want to have to set it up per domain - I want it to be for any domain that I set up with the common root directory to redirect to its own php of the same name. eg
mydom1.co.uk will direct to mydom1.php ,
another.co.uk will direct to another.php
thanks in advance for any help
You can put the following in your .htaccess file at the document root:
RewriteEngine On
RewriteBase /
RewriteRule .* %{HTTP_HOST}.php [L,QSA]
However, I expect you'll need to amend the main rule to handle the request of different pages. At the moment everything will be rewritten to /domain.php

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]

Ruby on Rails - .htacces RewriteRule to change image path

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]

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.