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

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.

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]

RedirectMatch file not found

So, I have in my web root an admin.php file and I want every time someone writes /admin to be redirected to that /admin.php.
I wrote in my htaccess
RedirectMatch 301 ^/admin$ /admin.php
However, I get an error from apache "file not found".
I created an empty /admin folder and it works...
Then I try to have an other (also non existant folder) to redirect to admin.php. So I write
RedirectMatch 301 ^/blah$ /admin.php
and it works...
Finally I delete the admin.php and I have the thing redirected to another .php
RedirectMatch 301 ^/admin$ /index.php
and it works again...
I understand that for some reason apache messes things up when the folder to be redirected from has the same name with the .php
With Rewrite I can do the redirection without any problems but I was wondering if it was also possible with just a Redirect... Maybe I am missing something here...
apache messes things up when the folder to be redirected from has the same name with the .php
This is due to Apache's content negotiation module that runs before mod_rewrite, mod_alias and makes Apache web server match extensions of files. e.g. /file can be in URL but it will serve /file.php.
To fix this behavior disable MultiViews by placing this rule on top of your .htaccess:
Options -MultiViews

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]

How to redirect to subdomain but then allow normal use of site

So I have my site, www.domain.com.
For a week or so I want to direct all traffic going direct to the site to subdomain.domain.com, a little promo page about an upcoming feature. I want visitors to then be able to continue to the site as normal though after they've read it, so a continue to www.domain.com/index.php link.
How can I do that with in the htaccess file? Everything I've tried so far messes up when clicking the continue link.
Thanks
with .htaccess you could use a 302 temporary redirect, but it would be for a whole sub folder as far as I know.
Another way would be to redirect with JS/server site language to the subdomain, create a cookie, then redirect back to www.domain.com/index.php .
the 302 redirect is explained here: How do I redirect my site using a .htaccess file?
You would need to have a .htaccess for the root folder point to your subdomain
Note that this is only possible if you enable mod_proxy in the Apache config of domain.com otherwise URL will change after redirect.
Enable mod_proxy, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^index\.php$ http://subdomain.domain.com/ [L,NC,P]

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]