htaccess redirect stuck in loop - apache

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]

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]

The folder "www" doesn't seem to work properly on hostgator

I've developed a website using Yii framework and now I need to move it to hostgator cheap hosting for a single site. It looks like it expects that the website must be placed into the root folder but my website has a www folder with index.php and resource files like js,css,images,etc. Also this folder contains a file htaccess with following content:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
But I suppose I have to add another htaccess into the root folder so all requests will redirect to the www folder. (I did it and tried different combinations of the rewrite rules but they didn't work properly). I'm guessing I should remove htaccess from the www folder and leave only one in the root folder.
Sorry guys, I always have problems with htaccess even after reading documentation.
Thanks in advance.
I think that you can configure your domain as an addon domain with the document root set in www as you prefer.
Of course I am presuming that you have cPanel on Hostgator.

How to redirect everything to a directory?

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

How to redirect all files in a directory to another in root via htaccess

I want to redirect all files (whether it exists or not) in /user directory on my site to a file named temp.php in root directory via .htaccess.
For example, if a user enter user/send.php or user/ or user/send (It may be that not Exists at all), all redirect to temp.php.
How can i do this ?
Try this:
RewriteEngine On
RewriteRule ^user/(.*)$ http://domain.com/temp.php [R,L]

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.