.htaccess Redirect images folder to another domain - php

ok so i have 2 domains
site1.com/images/ (all images)
and I need to redirect the imgages link site1.com/images/ to site2.com/images/ so that when
and when exp. site1.com/images/i.jpg is called it will look at site2.com/images/1.jpg and will find it.
.this is basic duplicate of the site and i don't want to move images back and forward.

Put in your .htaccess on site1.com
Redirect permanent /images http://sites2.com/images

If the websites are on the same server and running under the same user, you could use symbolic links between both websites.
E.g., create a symlink to /home/site2/public_html/images in /home/site1/public_html/images. Another option is using the Apache's Alias directive:
Alias /images /home/site2/public_html/images
Put this in the vhost config of site1.
Using redirection on Apache:
RedirectPermanent /images http://site2.example.com/images
The best way would probably fixing the HTML code pointing to one domain. If you cannot decide which domain should get the static content, create a subdomain (or even a different domain) to store the files.

Related

How to point the subdomain to Main domain sub folder

I have the site domain.com and creates the need subdomain.domain.com that points to the directory domain.com/public_html/subdomain
How do I do that?
The subdomain folder must be within the public_html folder domain.com
The vestaCP possesses the possilidade to choose the root folder of the subdomain?
Sorry for my bad English.
Simply put:
I need it:
sub.domain.com> /home/user/web/domain.com/public_html/subdomain
No:
sub.domain.com> /home/user/web/sub.domain.com/public_html
I always solve it by one of this 2 ways (each of them has advantages and disadvantages):
I redirect the subdomain by .htaccess file to the folder inder the main domain. So i just add Redirect 301 / http://example.com/subfolder to the .htaccess file of subdomain. It's classic unmasked redirection.
I make a PHP file index.php in /home/user/web/sub.domain.com/public_html folder with this content: <?php print file_get_contents("https://example.com/subdomain/"); ?> and in folder /home/user/web/example.com/public_html/subdomain is website. Phe PHP file displays the whole content of website on main domain folder.
None of them is the real solution, it's just a how to hack it, but I hope it will be useful

relative links work when accessing folder but not domain that references folder

I have a domain example.com that has a folder in it example.com/folder/ that has another domain that points directly to it so example2.com points to example.com/folder/
When I access this via example.com/folder/ everything works fine and I can access my css and js folders which are a level below the folder in example.com/css/ and example.com/js/
When accessing from example2.com I don't see the css or js on the site and I get a 404 when trying to link directly to them by clicking the link in the view source output in firefox.
I should also mention that I have no problem with PHP includes that come from below the folder example2.com points to, just with the relative URLs referenced in the HTML.
Any idea how to get the site to let me access these folders below the root of example2.com from both example1.com/folder/ and example2.com?
I'm using a LAMP system. Also, my goal is to keep the system flexible enough that I can use it in multiple sets of domain/subdomain without having to edit server files or PHP.
EDIT - I "solved" this by adding a field in my db for the root domain all the subdomains will stem from and making static URLs for the linked js and css. This doesn't seem like the most elegant way to deal with it but it's the best I can think up right now. If anyone has any better let me know!
Just a guess, but sounds like <base href=> is needed.
In your case:
<base href="http://example1.com/folder/_">
That will make all relative URLs request resources from example1, even if users come visit on your example2.com vhost. Though all links will then also redirect people there. It affects both JS/IMGs and clicky things.
From what you have described you folder organisation looks like this:
/path/to/domain/folder/
/css/ ^
^ |
| domain2.com
domain.com
PHP on domain2.com won't have a problem accessing ../css because it operates on the server, but a browser can't go further back then /path/to/domain/folder/.
I would suggest to create a symbolic link (if possible) on the server:
ln -s /path/to/domain/css /path/to/domain/folder/css
Alternatively, you could use Alias in Apache to set up aliases for domain2.com/css to /path/to/domain/css/
Lastly, a <base> tag could help you (as mentioned by mario), but any links followed from a page with such a tag will switch back to `domain1``.
In order to keep the system as flexible as I wanted it I decided to put both the example1 url into the database and the folder name so I can construct absolute links to the files I need to access in the HTML page. This also seems necessary with my mod_rewrite, which changes tries to tack the locations of these files to the end of the "pretty URL" (like example2/post/2/css/main.css). Thanks for your ideas you steered me in the right direction!

how to use htacces to restrict access to all files in a folder and it's subfolders and redirect to main?

So I'm on a 15-day trial of my (already paid) webhosting and they seem very good, at least they did until I got my FTP.
I have a php script that needs a folder outside of the root directory
Example:
website root:
/users/websites/public_html/ <- folder which users / browsers have access to
The script needs a folder here:
/users/websites/ <- above the root
for example /users/websites/sensetive_data/ <- browsers cannot acces this
but that's impossible on my web-host "because it's a shared hosting" <- their answer. And they can't change the root path.
So I cannot create any directories or files above /users/websites/public_html/
So, well, to no cancel my trial immediately, maybe I will try to do it in another way, I want to use htacces to restrict acces to a directory, and all the files in it and it's subdirectories,
So I can move the 'sensetive_data' folder to /users/websites/public_html/sensetive_data
I want it to redirect to the main page (so when accessing /users/websites/public_html/sensetive_data/* [http://example.com/sensetive_data/*] it will go to /users/websites/public_html/ [http://example.com/],
so even if the user knows the exact url, he/she will be redirected. How can I accomplish that?
If you want to do a redirect for a folder, say /users/websites/sensitive_data/
create a file in that folder called .htaccess and add the following (and specify the url to redirect to)
Options -Indexes
ErrorDocument 403 http://mysite.net/
In /users/websites/sensetive_data/.htaccess write:
Deny From All
For your whatever PHP script you need to change it yourself.

how to restrict and redirect from certain pages in php

I have a website that has an include folder which contains php templates and functions. If a user tries to access that folder. It may not harm website but I don't want my users to see those templates in an UN-organized manner. Instead, I want to restrict the user if he tries to directly access those files within include folder and redirect him to homepage.
Put this in an .htaccess file in that directory:
Deny from all
This is assuming you're using Apache or another web server that knows how to read and process .htaccess files.
For the redirect, instead of Deny from all you could try this instead:
RedirectMatch 301 ^/includes/$ http://www.yoursite.com/
You can configure the server such that this folder is not available to the public. Alternately, structure the site so this folder is below the siteroot - this makes it completely invisible to the public. Simply adjust your include paths and you're done. I prefer this solution, because the files are completely off the radar unless you are logged in and have access to the file system.

Multiple domains pointing to the same folder

I am currently running two websites. I am able to add my domains and set the root folder to / instead of /domain1.com and /domain2.com. That way both websites go to the same folder, however they both maintain their domain names (no redirects). My code determines whether the user is from domain1.com or domain2.com and displays the appropriate content using PHP.
Now, I have switched to another web hoster. The problem is that they don't allow you to specify where the root folder is: so it has to be /domain1.com and /domain2.com. They also don't seem to allow access to httpd.conf to edit VirtualHosts.
I have tried using .htaccess to do a redirect, but the problem is that when I go to domain2.com, it redirects straight to domain1.com, and it doesn't keep its host name of domain2.com.
I have also tried setting up symlinks, but it seems to be doing the exact same thing.
Is there any way to solve this?
Can you do a rewrite rule similar to the following (don't trust my syntax)?
# if domain2.com, send all requests to domain1.com
RewriteEngine on
RewriteRule ^(.*)$ ../domain1.com/index.php/$1
So if someone does go to domain2.com, all requests are passed through the index.php file on domain1.com for processing (I presume you're doing something similar already).
You should be able to set up symbolic links using ssh that don't redirect to the other domain. I have several .co.uk domains that use the same data as the .com that don't redirect. What host is it? You should be able to do this, maybe email them asking why it redirects.
Sure it's not in the htaccess file to redirect the domain2.com to domain1.com?

Categories