How to setup .htaccess to rewrite to a different folder - .htaccess

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]

Related

301 Redirect - Different Location for Root and Subfolders

I have a Domain which I want to redirect to New Domain using 301 redirection on htaccess; I did that successfully but now the problem is, I want to redirect pages / folders of old domain to various locations of new domain. E.g.
www.olddomain.com redirect to www.newdomain.com (works)
www.olddomain.com/about should redirect to www.newdomain.com/who-we-are (doesn't work)
I tried redirect 301 oldurl newurl on my htaccess but it won't work as root already redirects to new domain.
is there any way to solve this issue?
Certainly what you ask is possible. Most likely you had an issue with the order of your rules if things did not work for you. You need to keep in mind that rules are processed from top to bottom. So more specialized, more detailed rules have to come first, more general rules further down.
Here is an example for the single example you gave:
RewriteEngine on
# this is a specialized rule for a specific resource
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/about/?$ https://newdomain.com/who-we-are [R=301]
# this acts as a fallback for a request not yet redirected
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/?$ https://newdomain.com/ [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
These rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
You can use this :
RewriteEngine on
#redirect specific pages to specific location on new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?about/?$ http://newDomain.com/who-we-are [NC,L,R=301]
#redirect the old domain root to new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?$ http://newDomain.com/$1 [NC,L,R=301]
You can remove the %{HTTP_HOST} condition in both rules above if your domains (old and new) are on different servers or pointing to a different document root on the same server.
Thanks, everyone, issue is resolved.
RewriteEngine on
RewriteCond %{HTTP_HOST} hitechos\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ https://www.hitechdigital.com/ [L,R=301]
Regards
Manoj Soni

Using wildcard subdomain on specific directory

I am setting up an existing wordpress install to use multisite. I have successfully set up the network and since it was an existing install it uses subdomains (which I wanted anyways).
I have created a new site/subdomain on my network so it would look something like this example.domain.com
The issue I am running into is getting wildcard subdomains setup on my hosting provider BlueHost.
I am hosting multiple domains so when I setup a wildcard it always points to the root or original ip address.
I need it to point to a specific directory of the root so it uses the correct wordpress install. root/specific-directory
Is there a way to achieve this? I am not too familiar with a and cname issues. If there is a way to achieve it by writing in the htaccess that would be okay too.
I am hosting multiple domains so when I setup a wildcard it always points to the root
I'm assuming you meant that you get the same content for example.domain.com as if you were visiting www.domain.com. This can surely be handled through htaccess rewrite rules.
If a sub-domain named subsite was to be served out of ${DOCUMENT_ROOT}/subsite directory, you would add the following rules to your .htaccess placed at your web root / directory:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subsite\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subsite [NC]
RewriteRule ^(.*)$ /subsite/$1 [L]
If there are several mutisite subdomains that need redirection, use the following dynamic rules instead:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*?::\1
RewriteRule ^(.*)$ /%1/$1 [L]
You would also need to add ServerAlias *.domain.com to your <VirtualHost> configuration in Apache's httpd.conf file.
It seems BlueHost doesn't take up any requests to modify httpd.conf directly for you. But, they do provide a way to setup a wildcard subdomain from their Control Panel.
Take a look at How to Set Up a WordPress MultiSite in Bluehost. The last section outlines the process with screen shots. You also need to enable theme per site for your subdomains.
1- You can add your subdomain as an addon domain and it can have a separate subdirectory as you want.
2- Another way to achieve this is using another host for subdomians. On the second host you set up your subdomains and point your subdomains from your main host to subdomains on the secondary host.

.htaccess redirect dynamic domain to folder

For local development I've setup my router to redirect all requests to *.lan to my local xampp server. Now I have to setup virtual hosts for every domain I want to use. This works, but I don't want to setup a new virtual host (pointing to the corresponding folder) for every project and restart apache.
Is it possible to create a .htaccess file to "catch" a domain, and rewrite it to the folder, so I can use the domain as the folder name? For example:
example1.lan loads the website located in /example1/
example2.lan loads the website located in /example2/
This tread ( Apache - Domain for localhost to access folders as http://folder.local ) does almost the same thing, but with a redirect. I don't want a redirect, I want rewrite.
Thanks in advance!
Try adding these rules to the htaccess file in your document root (the directory where the example1 and example2 directories are):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.lan$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L]

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

Creating SubDomains to Absolute Paths with .htaccess

Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:
/home/user/public_html automatically redirects to http://www.example.com
Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'
/home/user/subdomain needs to redirect to http://subdomain.example.com
How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess
I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]
I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.
Good luck!
Try this rule:
RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]
But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.

Resources