Where is a 302 temporary redirect code located? - apache

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

Related

Setting up htaccess redirects

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)

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.

Ad-hoc shortened URL using Redirect / Rewrite?

I want to print a short, easy-to-type URL on paper brochures.
So that people can type example.com/foo into their smartphone browser, and the browser will display an existing page, say http://example.com/bar/yada.php .
I see that most pages about modrewrite involve regex, but what if I only need manually defined single pages?
Should I have an actual foo directory in the web root, containing a .htaccess file?
The following did what I needed, placed in the .htaccess at webroot.
An actual foo directory need not exist.
RedirectMatch 301 "(?i)^/foo$" "/bar/yada.php"
RedirectMatch 301 "(?i)^/foo/$" "/bar/yada.php"

Apache cross domain 404

I want to make apache to always open up a signle page for 404 errors from all subdomains.
The problem is that my subdomains are located in subfolders in public_html, and thus have a different root path.
For example the main domain this works quite well:
ErrorDocument 404 /Error/404.html
The Error folder and the main domain are located in public_html respectively.
However for the forum subdomain, located in public_html/forum/ the above root path does not, and it actually looks for public_html/forum/Error/404.html which doesn't exist.
I tried to rewrite rule for the forum folder, but it didn't work out either:
ErrorDocument 404 /../Error/404.html
Seems, it cannot go below the root folder for some reason.
Any ideas how can I refer to the same page from the main and the subdomain alike, without triggering redirects? (eg: http://mysite/Error/404.html would accomplish this, but would also change the url address of the page which I don't want)
Seems, it cannot go below the root folder for some reason.
Because being able to traverse above the document root is a very, very serious security risk. If your webserver gets compromised, people would be able to serve all kinds of files anywhere on your entire server.
If you have access to server config you can setup aliases for the /Error folder. For example, in your forum subdomain's vhost config, you can add:
Alias /Error /path/to/public_html/Error/
This way, when you go to http://forum.yourdomain.com/Error/404.html you'd actually be looking at http://main.yourdomain.com/Error/404.html. Then you can just use:
ErrorDocument 404 /Error/404.html
like normal in your forum subdomain.
But if you don't have access to your server/vhost config, you'll need to use mod_proxy and mod_rewrite. So in the htaccess file in public_html/forum/, add these to the top:
RewriteEngine On
RewriteRule ^Error/(.*)$ http://main.yourdomain.com/Error/$1 [L,P]

What's causing this forced redirect?

I do not have root access to the site site.com but I have access to a subdomain me.site.com
I add an index.php (or any file and or folder for that matter) to the root folder (public_html) of this subdomain but everytime I try to go to me.site.com/index.php I get redirected to www.site.com
The .htaccess file in this subdomain is currently blank.
Why might this be happening?
Thanks!
I'm afraid that .htaccess in subdomain folder can't override the .htaccess in root, and at the moment the .htaccess in your root folder is current affecting the subdomain, in which it redirects all visits to the subdomain to root. Imagine that .htaccess from your subdomain folder redirect visits to the directory itself and .htaccess from root also redirects visits to subdomain back to the root itself, that's gonna be a mess.
Well, that's what I think about it in my opinion. Have you tried contacting the one who own root folder to consider about this?