I would honestly appreciate any input, I have beent rying to do this for 2 days! I have 2 servers, one is my webserver w/Plesk running on a Digital Ocean droplet - another running a game server.
I was wondering if it's possible to use a sub-domain on my droplet and redirect to a sub-folder in /var/www/html/somefolder on my game server. I currently have my sub.domain.com configured with an A record to my game server's IP.
My question is, how can I make use of my .htaccess files to make this happen?
I've found a solution in case anyone else comes across this issue :D
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.yourdomain\.com
RewriteRule ^(.*)$ http://www\.yourdomain\.com/subdomain/$1 [L]
Related
I'm setting up a website with Digital Ocean. I want to have the home subdomain of my website handled by my webserver at home, and everything else handled by my Digital Ocean webserver.
So I added an A record pointing home.mywebsite.com to my home network's IP address. This works fine. But since updating the .htaccess file on my Digital Ocean webserver (in the /var/www/html directory), I've noticed that it is interfering with things on home.mywebsite.com. I have a Java application running on my home computer that loads classes from home.mywebsite.com/classes/ (with URLClassLoader) but it gets a ClassNotFoundException whenever the .htaccess file is on my Digital Ocean webserver.
Maybe I don't understand how DNS resolution works exactly, but I assumed that when I add an A record, the request will be forwarded before it is handled by my Digital Ocean webserver? So why would .htaccess even be effecting anything on my home webserver?
The .htaccess that is causing problems simply forwards a different subdomain (example) to mywebsite.com/example.
This is my .htaccess file on my Digital Ocean webserver:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.mywebsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)$ example/$1 [L]
I have a wordpress hosting account. I also have a standard web hosting account with the same host. My main website, domain.com is hosted on the Wordpress platform, BUT now I want to add a subdomain for sub.domain.com.
I can't host the script on sub.domain.com as part of the wordpress hosting, so I have been told to create it on my standard web hosting account and then use an A record in the DNS for domain.com to point to the IP of sub.domain.com.
Now, all this works if I visit http://sub.domain.com. However, www.sub.domain.com doesn't work.
What I want to know is, can I edit the .htaccess for domain.com to redirect anyone who visits www.sub.domain.com to http://sub.domain.com?
You can use this:
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com [NC]
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]
You can't use .htaccess to redirect traffic from www.sub.domain.com to sub.domain.com, because that traffic won't ever get to your server unless your DNS is set up correctly. In other words, if there's no DNS record for www.sub.domain.com, traffic looking for that address will never hit your server, so what is in your .htaccess would be irrelevant.
Keep in mind that you don't have to have www.sub.domain.com - almost nobody will visit that unless you share links using that domain yourself.
That said, if you really want to do this:
Create a sub-sub domain by creating an A record for www.sub.domain.com
Edit the .htaccess file for that sub-sub domain only, adding this rule:
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]
I am setting up two wordpress sites on one shared hosting plan for a client. 123-reg says on the decription that many sites can be hosted on the same server so I thought this would be a simple click of a button thing but clearly not.
I have two sites sitting in 2 separate sub directories; FolkstockFestival and FolkstockArtsFoundation. I also have two domains www.folkstockfestival.com and folkstockartsfoundation.com. I need each domain to go to its corresponding sub directory.
I thought you'd be able to do this with the DNS settings but I haven't found a way to do that so I think .htaccess is the way forward. With mod_rewrites, is there a way to direct the user to the right directory depending on which domain is requested? The .htaccess file would be in the root directory.
I do not want the sub-directory to show on the domain so www.folkstockfestival.com shows as is but directs to the correct site.
Many thanks
Use DNS to make both of your domains pointing on your server, and use this htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} www\.folkstockartsfoundation\.com [NC]
RewriteRule ^(.*)$ /FolkstockArtsFoundation/$1 [L]
RewriteCond %{HTTP_HOST} www\.folkstockfestival\.com [NC]
RewriteRule ^(.*)$ /FolkstockFestival/$1 [L]
I am a novice with server side coding so bear with me if the answer is obvious to some:
I designed the front end of a website sometime ago. It resides in a shared hosting environment as a subdirectory. I am trying to upload an .htaccess file in order to redirect to its mobile version ( that I did not design and that is hosted on another account). I have tried uploading the .htaccess file to both the root directory and the subdirectory and each have their issues.
When I upload to the root the redirect does not occur on any devices. It was not my wish but I was expecting that all websites under the root would be redirected to the mobile version of this one site but nothing occurred.
When I upload the .htaccess file to the subdirectory - viewing from all devices returns "The page you requested is NOT AVAILABLE"
The following code is what I was provided by the mobile designers to input into the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
#Check if this is the desktop to mobile query string
RewriteCond %{QUERY_STRING} (|&)m=1(&|$)
#Set a cookie, and skip the next 2 rules
RewriteRule – [CO=mredir:1:%{HTTP_HOST},S=2]
#Check if this is the mobile to desktop query string
RewriteCond %{QUERY_STRING} (|&)m=0(&|$)
#Set a cookie, and skip the next rule
RewriteRule – [CO=mredir:0:%{HTTP_HOST},S]
RewriteCond %{HTTP_USER_AGENT}
“android|blackberry|iphone|ipod|iemobile|opera
mobile|palmos|webos|googlebot-mobile” [NC]
#Check if we’re not already on the mobile site
RewriteCond %{HTTP_HOST} !m\.
#Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(|&)m=0(&|$)
#Check to make sure we haven’t set the cookie before
RewriteCond %{HTTP_COOKIE} !^.mredir=0.$ [NC]
#allow mobile sites to access images on the main site
RewriteCond %{REQUEST_URI} !\.(gif|png|jpe?g|ico)$ [NC]
RewriteRule ^(.*)$ http://example.mobi/ [L,R=301]
</IfModule>
I have ommitted the url that would replace http://example.mobi
I cannot detect if there are any syntax errors in the above code, any help would be greatly appreciated.
Thanks
It's just my guessing. But why do you urge to use .htaccess for simple redirects? If only one site (particular subdirectory on you root / hosting) should always redirect to new, mobile version, then why can't you set a permanent redirect using your server / hosting configuration?
Such tools are in nearly every control panel like cPanel, DirectAdmin etc. Even if your hosting provider runs its own, sophisticated piece of software for running and managing hosting server, I can hardly believe it that it not provide you with any tool or solution for making permanent redirects and thus forcing you to play this game with .htaccess.
BTW: If you 100% sure that you can't do this via server / hosting configuration, and .htaccess is the only way, than maybe this is the right moment to change your hosting? After all, domain / address / URL is the most important for each project, and it can be hosted on any hosting.
As for your problem, I would advice to run second scenario (.htaccess in subfolder) from a regular PC or Linux and via browser equipped with FireBug or simillar developers tool. Then enter your webpage that should do the redirect, but fails, and carefully examine that tool's log / path to see, where you're actually redirected? Is this trully URL you've been expecting? Most times "The page you requested is NOT AVAILABLE" means what it means, that is -- a website tried to redirect your browser to an non-existing URL.
I'm moving my site to a new host but I need to have my current server continue to handle requests (not all files can be moved to the new server).
So I added a parked domain to my old server (old.mydomain.com) and I want all requests to it to be written to the files from the old site.
My old site (mydomain.com) was hosted internally in a folder (/public_html/mydomain/) and I want all requests to old.mydomain.com to be rewritten to the same folder.
So if mydomain.com/blog was internally at /public_html/mydomain/blog, I now want old.mydomain.com/blog also to reach /public_html/mydomain/blog.
Here is the .htaccess that I'm trying to use:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com/*
RewriteRule ^(.*)$ mydomain/$1 [NC,L]
But for some reason as soon as I add the $1 in the rewrite rule I get an internal error.
Any ideas?
Configure this as a separate vhost called old.mydomain.com and ensure it comes before *.mydomain.com in your vhost definitions (i.e. higher in vhosts.conf). Give old.mydomain.com the same DocumentRoot as your previous domain had.
.htaccess is the most processor intensive way to serve a webpage, and should only be used there are no other options available.
Please try to fix your .htaccess config as follows:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com$
RewriteRule ^(.*)$ /public_html/mydomain/$1 [NC,L]