I have a domain, let's make it example.com, and a subdomain, sub.example.com. For the subdomain I have a SSL certificate, for the main domain I don't. On my server I have a .htaccess file for my main domain, but none for my subdomain. How do I specify, in this file, to forward from http://sub.example.com to https://sub.example.com? Or do I need a second .htaccess file in the folder for my subdomain?
I assume your subdomain is hosted in a subdirectory off the main domain's document root? So the main domain's .htaccess file is in the parent directory? (If not, then you will need another .htaccess file in the root of the subdomain.)
Try the following mod_rewrite directives at the top of the main domain's .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
This is a temporary (302) redirect. Change it to 301 only when you are sure it's working OK. (Permanent redirects are cached hard by the browser so can make testing problematic.)
You only need one RewriteEngine directive at the top of your file.
UPDATE: If the file structure is as mentioned above (ie. the subdomain is located in a subdirectory off the main domain's document root - where the .htaccess file is located) then you will need to use the REQUEST_URI server variable to reference the URL-path, not a backreference (ie. $1 - as I had initially) to the captured RewriteRule pattern, since the captured backreference will contain the subdirectory, which is not part of the URL-path.
Related
I am trying to redirect all requests to the root (without www.) of a domain that I am managing to the www. address (e.g.: example.com -> www.example.com). I have modified my .htaccess file and I have managed to get requests to the root domain with a path to the www. domain (e.g.: example.com/somepath/ properly directs to www.example.com/somepath/). However, requests to the root domain without a path or trailing slash are not redirecting properly (e.g.: example.com !-> www.example.com). So, it seems like I have things working correctly when there is a trailing slash, but not when there is not (e.g.: for the root domain). I have included the contents of my .htaccess file below. Thanks in advance!
RewriteEngine On
RewriteBase /
# Redirect paths of non-www to www
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]
So I have a file
www.example.com/for/ldt.php?do=test
But this file can be found on www1.example.com, www2.example.com and so on
I want to redirect all users no matter what www subdomain the person is coming from to a single subdomain of cdn.example.com
The kicker here is I dont want to redirect for all files or requests, only if they are trying to access ldt.php which is a dynamic script so it can have a bunch of arguments too
Finally, when the change is made with htaccess, is it possible for the url to physically change in the address bar too?
Can all this be done?
You can do it using a rewriteRule that redirects the file based on HTTP_HOST headers or only if the requested host is not cdn.example.com .
RewriteEngine on
RewriteCond %{HTTP_HOST} !^cdn\.example\.com$ [NC]
RewriteRule ^for/ldt\.php$ http://cdn.example.com/for/ldt.php [L,R]
This will automatically append the querystring ?do=test to the end of the destination URL.
I've added a 2nd domain in my hosting account, and .htaccess handles requests to this 2nd domain. However, a problem arises if the URL ends with a folder name and without a trailing slash. The resulting URL will contain the domain + the subdirectory (in effect, twice the domain name).
For example, http://domain2.com/demo is rewritten to http://domain2.com/domain2.com/demo/.
There is no probem if the URL already has a trailing slash, example: http://domain2.com/demo/.
The directory structure in my account is such that the files for that second domain live in a subdirectory of the main domain.
- main-domain.com
- public_html
- domain2.com
The .htaccess resides in the public_html folder and file contains this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain2.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/domain2.com/
RewriteRule ^(.*)$ /domain2.com/$1 [L]
I've tried many variations of these lines but I either get the same results or a server error.
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
I was wondering how to exclude a subfolder from .htaccess redirect.
I have an .htaccess file in the root of my old domain and I have the following in it:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This of course redirects any traffic from the old domain to the new one. But I want to exclude a directory from this rule. Meaning www.olddomain.com/media/videos should not redirect to www.newdomain.com/media/videos
The reason I want the exclusion is because I'm hosting static files such as videos on a shared web server (old domain) and I'm keeping everything else the site needs on the new VPS server (new domain).
Just put RewriteCond %{REQUEST_URI} !^/media/videos above your RewriteRule. It checks wether the URL starts with /media/videos and if it does so, the RewriteRule will not be met.