Retrieving files from another URL on the server - .htaccess

I have one server two domains. I need the images and css apply to both domains. Currently I solve it using htaccess:
RewriteRule ^.*\.(css)$ http://www.rs.cz/style/$0 [L,R]
How is it possible to improve? Or at least refer to a relative path on the server?

Create one subdomain just for hosting images and static content like:
images.mydomain1.com
and another subdomain like:
images.mydomain2.com
Have both of them redirect to the same directory in the server like /var/www/common_images.

Related

Redirect old domain to new website via htaccess?

Have an old webisite (oldwebsite.com) and want to redirect all the domain requests to the new website (newwebsite.com) via htaccess (probably).
How would you go with it? Need like all old pages to redirect to the new website homepage as the structure and pages are entirely diffirent.
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^ https://new.example.com%{REQUEST_URI} [QSA,R=301,END]
In general I would recommend to implement such general rules in the http server's central configuration of the host "old.example.com". You can instead use a distributed configuration file (".htaccess"), but that comes with a few small disadvantages.

Trying to obtain images from another location but RedirectMatch in htaccess is ignored

I've just setup a new subdomain 'preview', ie preview.example.com and I am setting up a replicate of the codebase of a live website (from the www. subdomain) so that I can have a duplicate environment for testing and showing my client recent changes.
I want to save myself having to copy over all the images to the preview and so the normal location under which images are saved in a WordPress site /wp-content/uploads/ is empty. I wanted to ues a simple htaccess rule to get these instead from the live subdomain where the image at the same path will exist. In other words, a request for
https://preview.example.com/wp-content/uploads/2021/01/test.jpg
would be redirected to the following where the image exists..
https://www.example.com/wp-content/uploads/2021/01/test.jpg
I've tried to set this up with the simplest of htaccess redirect rules but it just seems to be ignored and I've no idea why. Any idea how I can troubleshoot this?
RedirectMatch 301 /wp-content/uploads/(.*) http://www.example.com/wp-content/uploads/$1
Converting my comment to answer so that solution is easy to find for future visitors.
Scenario is that both subdomain and main domains are on same host but their DocumentRoot are set to different paths. OP wants to serve lot of images from subdomain but doesn't to do bulk copy.
Suggested approach without tinkering with .htaccess is to create a symbolic link of wp-content/uploads/. Only option suggested to use in .htaccess is:
Options +FollowSymLinks
Which allows use of symbolic links for serving web requests.

Fake subdomain for different users while still browsing same folder structure

I'd like to create fake subdomains for different users for more vanity, and to make the user (in this case a company) feel they are in a more isolated environment.
For the sake of maintainability, it's important for me that all users still browse the same files, to avoid having to update files for every single user that exists when updating the code.
My website has one public part at root, let's say www.example.com. I'd like to be able to fake the following kind of subdomains:
user1.example.com
The true URL would be www.example.com/member/?user=user1. I'd like for the folder structure to follow the same pattern. www.example.com/member/settings/?user=user1 would appear as user1.example.com/settings/ and so on.
I assume this would best be achieved with .htaccess, no?.
What is the proper .htaccess code for this?
Thank you!
based on the information you've provided here's a bit of .htaccess not tested but might do the job.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=(.*)$
RewriteRule ^(.*)$ http://%1.example.com/$2? [R=302,L]
Once you find it working fine, replace 302 to 301
Note: It's important to note that you'll need to add a subdomain wildcard
In the end, this was super easy to solve! It wasn't solved the way I first expected though, using .htaccess. I solved it with something called wildcard subdomain.
When you register a new subdomain, enter * in the domain prefix, such as *.example.com. A folder for the wildcard subdomain will be created on your server, such as _wildcard_.example.com. Whenever you access site1.example.com, fakesub.example.com etc, the browser of the visitor will read the files in the wildcard.example.com folder.
The beauty of it all is that if I create a certain subdomain that I want to use, for example forum.example.com, this real subdomain will have priority over the wildcard subdomain, and files will be fetched from the folder for this subdomain, as opposed to from the wildcard subdomain folder.
I use PHP and need to know the subdomain to fetch the appropriate database for the current user. To do this, I use the following code:
$subdomain = explode('.', $_SERVER['HTTP_HOST'])[0]
With a wildcard SSL cert I have all of these subdomains secure.

mod-rewrite rule for sitemap.xml

I am using a CMS that hosts multiple websites simultaneously and I am seeking a method to properly serve sitemap.xml files for each site. I have concluded that a mod-rewrite rule will be the most effective solution for this.
My idea is to have a /sitemaps/ directory with the site map for each domain named as the domain name, i.e. /sitemaps/acmewidgets.com.xml
Since the appropriate sitemap url would be acmewidgets.com/sitemap.xml I need assistance with creating a mod-rewrite rule that will map this accordingly.
Thank you!
RewriteRule /?sitemap.xml %{DOCUMENT_ROOT}/sitemaps/%{HTTP_HOST}.xml [L]

Display content as root from subdomain

I have a site running on a subdomain of new.site.com and I want to be able to just put something in the .htaccess file that will display the subdomain's content as if it was in the root.
Where the site is: new.site.com
The url that I want to use: site.com
Basically what I want to do is just not move the files from the subdomain to the root of my server.
Try using a domain pointer.
EDIT:
Not sure why my answer was downed. Let me elaborate a little more.
I do this myself on my websites. In my hosting company's control panel, I have the option to create domain pointers. What this does is set a domain to use the website of another domain.
Actually found this that works incase anyone was wondering:
RewriteEngine on
RewriteBase /
RewriteRule ^$ /subdomain/ [L]

Resources