How to redirect everything to a directory? - apache

I am organizing my public_html directory. I copied all scattered files and directories from root public_html into public_html/main directory. I would like to redirect all requests into "main" directory, basically just adding "main/" to the URL. For example:
...mydomain.com goes to ...mydomain.com/main
...mydomain.com/product_page.php goes to ...mydomain.com/main/product_page.php
*notes: Just replace the ... above with "www." because I am not allowed to post links.
I tried this rule below in .htaccess but doesn't work.
RewriteEngine On
RewriteRule ^main/?$ http://www.mydomain.com/main/ [R=301,NC,L]
Is there something wrong with that?
Thanks.
hc.

RedirectMatch 301 /(.*) /main$1

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]

.htaccess directory wildcard redirect

Please can someone tell me why this isn't working?
I am trying to redirect all files within the directory "Kent". In this example the page to be redirected is "ramsgate" and change the folder structure.
from:
/electrical-contractors/kent/ramsgate.php
to:
/electrical-contractors-ramsgate/
Using a wildcard redirect, why doesn't this work?
RedirectMatch 301 /electrical-contractors/kent/(.*).php /electrical-contractors-$1/
Have this rule inside /electrical-contractors/kent/.htaccess:
RewriteEngine On
RewriteRule ^(.+)\.php$ /electrical-contractors-$1/ [L,NC,R=301]

.htaccess - how to redirect from root to subfolder?

I have a website with URL www.somedomain.com
I want to redirect to a subfolder "folder", so that when you enter the website you are automatically redirected to www.somedomain.com/subfolder.
But I do not want the root directory to be changed, just simple redirection.
Is this possible with .htaccess ?
Is it possible to hide the /subfolder part from the URL also ?
Thanks
You can use a rewrite rule that uses ^$ to represent the root and rewrite that to your /store directory, like this:
RewriteEngine On
RewriteRule ^$ /store [L]
Just replace store with your directory

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?