Apache Rewrite for images on subdomain - regex

I have a webserver and a subdomain. All of my images are stored in /public/images within my site directory for www.mysite.com. However I have a separate site directory for testing beta.mysite.com however on this page with a different git branch all of my images are broken because I did not want to copy all of the images. Is it possible to say for all image requests or for all 404 requests try looking at mysite.com?
I have found an example on another questions
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^images/(.*)$ http://example.com/sub_ds/test/images/$1
But since I am rather new to mod_rewrite im not sure what is going on or how to manipulate it to work for me.

You can use this rule in root .htaccess of beta subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} =beta.example.com
RewriteRule ^(images/.+)$ http://example.com//public/$1 [L,NC,R=301]

Related

How to redirect all subdomain urls to main domain

For some reason search engines are indexing my addon domains on my hosting. They should not do that.
For example I just found urls like
addondomain/maindomain.com
how to prevent this happening? How did search engines even find my addondomains?
What is the solution here? I tried this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com
RewriteRule ^(.*)$ http://www\.maindomain\.com [L]
but when I visit the url for example
addondomain/maindomain.com for example nothing happens?
Try to Change in .httaccess file. You can replace your rule with this rule::
RewriteCond %{HTTP_HOST} =shop.domain.abc
RewriteRule ^ http://www.domain.abc/? [R=301,L]
Using %{REQUEST_URI} will cause original URI to be copied in target. Trailing ? in target will strip off any pre-existing query string.
This answer is a further explanation to my comment on your question.
how to prevent this happening? How did search engines even find my
addondomains?
Google can easily find these subdomains on your site. To prevent this from happening, you can set a redirection with a 301 status code to inform Google that it should not index the addon domain. By doing this, Google will update its index as well.
This is a very common scenario with shared hosting and specially when you use CPanel. In Hostgator's support pages, you can see they have mentioned about this behavior.
Addon URL Example
For the primary domain abc.com, if you assign the addon domain 123.com
to the folder "123," the following URLs would be correct:
abc.com/123
123.abc.com
123.com
All three of these paths would access the same directory and show the
same website. For visitors going to 123.com, there is no evidence that
they are being routed through 123.abc.com.
https://support.hostgator.com/articles/cpanel/what-is-an-addon-domain
You can fix this by adding the following to your .httaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
NC - match in a case-insensitive manner.
R - causes a HTTP redirect to be issued to the browser. When given as
R=301 it will be issued with a 301 status code which is required to
inform Google that their index should be updated accordingly.
L - Causes mod_rewrite to stop processing the rule set. In most
contexts, this means that if the rule matches, no further rules will
be processed.
===========================================================
Edit: Updated to add redirection to all domains, as requested in the comments.
To do this, you can simply check if the hostname is equal to your main domain, and if it's not, redirect it to the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
Hope it helps :)

Apache redirect keeping URL

I'm making a JavaScript web app running on an Apache 2 server. I'm wondering if it's possible (either with mod_rewrite or some other mod) to make any path you type load the index.html from the root path, but keeping the URL?
For example: "example.com/blah/blegh" will load "example.com/index.html", but the address bar will still have "example.com/blah/blegh". Same if you tried typing "example.com/everything/is/index" would still load "example.com/index.html" and have "example.com/everything/is/index" in the address bar.
A simple answer about any mods I would need to use and which commands might be best would suffice. Though a code example would be very useful since I'm new to regex's and Apache rewriting.
Thank you for your time :)
Note: I'm doing this since I'm using History.js to parse URLs/titles into the address bar and tab titles while navigating (a one-page dynamic site). I'd like to be able to just load up the root index.html with the user's initial URL request and respond to users' actions that way much like a REST server.
Actually, you want to rewrite without redirecting. This requires enabling mod_proxy and mod_rewrite in Apache's httpd.conf.
Then, the rewrite should look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [NC,L,QSA]
Reference:
What exactly does the Multiviews options in .htaccess?
htaccess rewrite without redirect
Apache: RewriteRule Flags

Redirect all requests to index.php except subdomains

I'm using AltoRouter: (http://altorouter.com/) and I modified .htaccess as suggested in the instalation to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
This is so that index.php can handle all the requests. My problem is that I am using addon domains in cpanel and I am having internal server errors when I try to access one of the domains associated with the addon domain.
Example:
My main domain is mainsite.com.
Then I have several sites:
site1.com that cpanel automatically associates with site1.mainsite.com and creates a folder mainsite.com/site1.com. So if I access site1.com I would see in browser site1.com but the content delivered would be the one inside the mainsite.com/site.com folder.
This works if I don't use the .htaccess rule but I need it for routing. If I have that rule I get internal server errors everytime I access site1.com (I assume that it's a conflict between cpanel rules and .htaccess).
How can I modify the rule so that it only affects maindomain and not subdomains? I am assuming that by doing this there would be no conflict and my problem would be solved.
I am really bad at .htaccess and regex but I am willing to learn if needed. I would still appreciate if you could point me to the right direction. (both in the idea and in good websites that can help me understanding this)
How can I modify the rule so that it only affects maindomain and not subdomains?
You can add a new condition based on host name:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?mainsite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Rewrite all requests through a directory

My site www.example.com has its document root to public_html directory. But, all of my content is hosted in public_html/www.example.com/. So, I want when a user visits my site he get contents from public_html/www.example.com/ and not public_html. So, what should be the .htaccess rewrite rule for that?
I am not good in rewrite rules but the best I found was:
RewriteRule !^blog blog%{REQUEST_URI}
There are two problems with this.
It will not work properly if user requested www.example.com/blog/
It will also not work when we use directory name as www.example.com
Give this a try and let me know if it works for you, and if it doesn't try to describe as detailed as possible what happens.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/www\.example\.com [NC]
RewriteRule ^(.*)$ /www.example.com/$1 [NC,L]

.htaccess forward local to remote on 404

I do all of my development locally on sites that use a CMS. We have a directory that is used to store user-uploaded content, such as images (/assets/). The problem is, when developing locally, I don't want all of the uploaded files from the production site on my machine, so I leave this directory empty, and all of the HTTP requests for files in the /assets/ directory get 404'ed.
What would be great is if I could have a rewrite rule in my .htaccess that detects the 404, and forwards to the external URL of the production site to load the asset from there. The logic would be:
Request localhost/somesite/assets/foo.jpg
200 response ? send the local file /somesite/assets/foo.jpg
404 ? forward to http://www.productionsite.com/assets/foo.jpg
Is this possible?
Have your .htaccess rules like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(assets/.*)$ http://www.productionsite.com/$1 [R,L,NC]
This will make sure that any request matching /assets/ and doesn't already exist on your local webserver will be externally redirected to http://www.productionsite.com/assets/...
btw you should really improve your acceptance rate otherwise you may not many answers here.
Is /assets always on another site, or only if the files are not available locally?
The reason I ask is that it may be simpler to always redirect just /assets to the live with the following (untested) rules in httpd.conf/.htaccess on your development server:
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 ^(assets) [NC]
RewriteRule ^(.*)$ http://www.livesite.com/$1 [L]