Redirect a forbidden URL in Apache - 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.

Related

How to redirect path to a different folder using mod_rewrite/mod_alias?

I have mod_rewrite and mod_alias enabled. I have some csv files saved in /home/ubuntu/csv. Due to reasons, I can't move files to any other folder.
My website url redirects to /var/www/html. So wesbite-url.com would redirect to /var/www/html.
What I want is somehow website-url.com/files/a.csv should redirect to /home/ubuntu/csv/a.csv? Basically I want to access any file, say x, saved in /home/ubuntu/csv via website-url.com/files/x
How do I go about doing this?
I tried the following commands among others:
RedirectMatch ^/files/$ /home/ubuntu/portalCsv/
RedirectMatch ^/files/ /home/ubuntu/portalCsv/
Redirect /files/a /home/ubuntu/portalCsv/
What is the correct command for this?
You need to capture the url part after /files/ and append it to the Redirect destination
RedirectMatch ^/files/(.+)$ /home/ubuntu/portalCsv/$1
This will redirect your browser from /files/foobar to /home/ubuntu/portalCsv/foobar .
if you want to redirect using mod-rewrite,you can use the following :
RewriteEngine on
RewriteRule ^/?files/(.+)$ /home/ubuntu/portalCsv/$1 [L,R]

https before address shows only files in private_html folder on host

when i try my webpage address with https, page shows only files in the private_html folder not public_html.
but my website runs in the public_html. for example i use an index.html file (including forwarder script) and when you type https://alirezah.net it redirects to http://alirezah.net.
how can i fix it? I wanna use cloudflare ssl service but this happens.
I tried to edit htaccess but it doesn't help
If your website is hosted on Directadmin, then follow these steps:
Log in to your DirectAdmin control panel
Click on Domain Setup
Click on the domain name you wish to change this for
Choose the Use a symbolic link from private_html to public_html - allows for same data in http and https checkbox.
Note the warning message - anything in private_html will be removed, so be sure you do not have content left here that you want to keep.
Accept the warning notice and your now pointing you are private_html directory to the public_html directory.
If your website is installed in private_html it will be deleted using this function. If you want to keep your website installed in private html and also redirect http to https, then you need to create a .htaccess file in public_html with the following contents:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://example.com/%{REQUEST_URI} [R,L]

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]

Redirect entire site except one directory to a new site - apache .htaccess

I am trying to move my current site over to a new domain, except for ONE directory.
Example:
current site: oldsite.olddomain.example
new site: newdomain.example
So I know I can create an .htaccess redirect entry to do this, and it works, but I want ONE exception - I do NOT want to redirect a specific directory. I still want this to work:
http://oldsite.olddmain.example/myspecialdirectory/… and every file and directory under it.
Can someone help?
Thanks!
Try this mod_rewrite rule in the .htaccess file in the document root of oldsite.olddomain.example:
RewriteEngine on
RewriteRule !^myspecialdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]

mod_rewrite to absolute path in .htaccess - turning up 404

I want to map a number of directories in a URL:
www.example.com/manual
www.example.com/login
to directories outside the web root.
My web root is
/www/htdocs/customername/site
the manual I want to redirect to is in
/www/customer/some_other_dir/manual
In mod_alias, this would be equal to
Alias /manual /www/customer/some_other_dir/manual
but as I have access only to .htaccess, I can't use Alias, so I have to use mod_rewrite.
What I have got right now after this question is the following:
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L]
this works in the sense that requests are recognized and redirected properly, but I get a 404 that looks like this (note the absolute path):
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
However, I have checked with PHP: echo file_exists(...) and that file definitely exists.
why would this be? According to the mod_rewrite docs, this is possible, even in a .htaccess file. I understand that when doing mod_rewrite in .htaccess, there will be an automated prefix, but not to absolute paths, will it?
It shouldn't be a rights problem either: It's not in the web root, but within the FTP tree to which only one user, the main FTP account, has access.
I can change the web root in the control panel anytime, but I want this to work the way I described.
This is shared hosting, so I have no access to the error logs.
I just checked, this is not a wrongful 301 redirection, just an internal rewrite.
In .htaccess, you cannot rewrite to files outside the wwwroot.
You need to have a symbolic link within the webroot that points to the location of the manual.
Then in your .htaccess you need the line:
Options +SymLinksIfOwnerMatch
or maybe a little more blindly
Options +FollowSymlinks
Then you can
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/site/manual/$2 [L]
where manual under site is a link to /www/customer/some_other_dir/manual
You create the symlink on the command line with:
ln -s /www/htdocs/customername/site/manual /www/customer/some_other_dir/manual
But I imagine you're on shared hosting without shell access, so look into creating symbolic links within CPanel,Webmin, or whatever your admin interface is. There are php/cgi scripts that do it as well. Of course, you're still limited to the permissions that the host has given you. If they don't allow you to follow symlinks as a policy, you cannot override that within your .htaccess.
AFAIK mod_rewrite works at the 'protocol' level (meaning on the wire HTTP). So I suspect you are getting HTTP 302 with your directory path in the location.
So I'm afraid you might be stuck unless.. your hosting lets you follow symbolic links; so you can link to that location (assuming you have shell access or this is possible using FTP or your control panel) under your current document root.
Edit: It actually mentions URL-file phase hook in the docs so now I suspect the directory directives aren't allowing enough permissions.
This tells you what you need to know.
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
It interprets RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L] to mean rewrite example.com/manual/ as if it were example.com/www/htdocs/customername/manual/.
Try
RewriteRule ^manual(/(.*))?$ /customername/manual/$2 [L]
instead.