Setting up htaccess redirects - apache

Does anyone know how I could achieve the following with .htaccess?
If the file does not exist but folder exists, redirect to folder
If the file is in the root directory, or folder does not exist, redirect to home page "/"
For example, if we have to take a request to www.domain.com/folder/file.html:
If /folder/file.html exists, we just show it as is, no redirect.
If /folder/file.html does NOT existing but /folder/ does exist, we redirect to /folder/
If then /folder/ does not exists we redirect to "/" We need this done in a generic way so that we do not need to hardcode the folder or file names into the .htaccess file.
Should work for all file types with a way to set up exclusions (both for folders and filetypes)

Related

Redirect a forbidden URL in Apache

I would like to move an entire directory and its subdirectories from one Apache server A to another Apache server B, and then create a redirection from A to B.
i.e.
http://server_a.com/docroot/dir to http://server_b.com/docroot/dir.
This is what I did:
I copied the files and directory structure under dir from A to B
I deleted the directory dir on A
I created a rule in docroot/.htaccess on server A that reads
Redirect permanent dir/ http://server_b.com/docroot/dir/
But when I go to http://server_a.com/docroot/dir/path/to/file/index.html, I get a 403 Forbidden, even if the target page http://server_b.com/docroot/dir/path/to/file/index.html is up.
I know that the .htaccess is read by Apache, because it correctly controls other parts of server_a. I am not root on these servers. I have also tried with a RewriteRule, with the exact same results.
How should I go about creating a redirect in this case? Thanks.
If you have mod_rewrite enabled than you can do this
RewriteEngine On
RewriteRule ^/?docroot/dir(/.*)?$ http://server_b.com/docroot/dir$1 [R=301,L]
Put it in your .htaccess file in the Document Root directory http://server_a.com/.
Note:
Delete you Browser cache first.
You'll need the leading / in the old URL. Like this:
Redirect permanent "/dir" "http://server_b.com/docroot/dir/"
See the mod_alias docs for more details.

Htaccess redirection including subdirectory

I need htaccess that will redirect users accessing subdirectories of specific directory (one that contains htaccess) to corresponding subdirectories on another server.
For example:
server_1/dir_with_htaccess/user_subdir_of_choice ---> server_2/user_subdir_of_choice
No subdirectory woudl actually exist on first server, it woudl just contain htaccess.

mod rewrite - redirect only to subfolders

I currently have my main page at www and some files at www/files/.
How could I block the access to /files itself, but allow users to access www/files/ without having to give each folder a separated .htaccess file and without changing my current htaccess file at www?

htaccess to redirect a specific path on any subdomains to a specific path on the main domain

I have one .htaccess file (all subdomains point at the same file directory as the main domain, so one .htaccess file would need to accomplish this). What I'm trying to do is this: any current and future subdomains should redirect like so:
sub1.example.com/cart
sub2.example.com/cart
sub3.example.com/cart
unknown_future_subdomain.example.com/cart
Those 4 subdomains and specific /cart path should all redirect to example.com/cart
I have tried different solutions but they caused redirect loops, I think because the sames .htaccess file is loaded on the main domain.

Where is a 302 temporary redirect code located?

Where would a 302 temporary redirect code located within a website's files? I am struggling to find it and would like to change it to a 301 permanent. I tried .htaccess which I thought would be the most obvious location for it.
Are you trying to redirect the whole site or just one subdirectory?
If the whole site, you'll need to edit the .htaccess file in the root of the site. If a directory, you'll need to create an .htaccess file in that directory that will then control everything below that directory.
This is a simple 302 redirect