htaccess redirect where subdomain - .htaccess

I have a site example.com. Any traffic to www.example.com is redirected to example.com in the .htaccess file using:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
This piece of code that I swiped of the interweb works fine.
I have now added a subdomain subdom.example.com. Similar, any traffic to www.subdom.example.com should be redirected to the non www canonical version.
The following code does not work:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://subdom.example.com/$1 [R=301,L]
Presumably redirects work a little differently where subdomains are involved. Could anyone share how I would edit the above snippet to redirect any www. traffic to the canonical non www. subdomain version?

I believe you don't have sufficient RewriteCond conditions for both redirects. What is happening is that first one is unconditional redirect and since it appears first it always fires and 2nd one never fires.
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Above code will work with both of your domains.

Related

Redirect subdomain of one domain to a subdomain of another domain

I am trying to redirect the following (respectively):
http://sub.firstdomain.com/d/(all_files_and_folders)
http://sub.firstdomain.com/d2/(all_files_and_folders)
to
http://sub.seconddomain.com/d/(all_files_and_folders)
http://sub.seconddomain.com/d2/(all_files_and_folders)
There are other files and folders under the first subdomain that I do not want redirected. Previously this was working but now it seems like Go Daddy changed something and what I had is no longer working. Here it is:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^stats/(.+) /dstats/count.php?statspage=$1 [L,NC,QSA]
RewriteRule ^(.+)\.deb$ /dstats/count.php?file=$1.deb [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(d2?)/(.*)$ http://sub.seconddomain.com/$1/$2 [L,R=301]
You can ignore the RewriteRule. It is working fine. I wanted to make sure I included the entire .htaccess file just in case. My issue is with RewriteCond it seems.
The following should work:
RewriteEngine On
Redirect 301 /d/ http://sub.seconddomain.com/d/
Redirect 301 /d2/ http://sub.seconddomain.com/d2/
The above should only redirect anything in the /d/ and /d2/ folder of the sub subdomain.
EDIT: Second try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1\.
RewriteRule ^(d2?)/(.*)$ http://sub2.mydomain.com/$1/$2 [L,R=301]

.htaccess not working properly while setting SSL redirects only on certain pages

While looking for an answer for the question above I have encountered this script as a solution(I would like to have my website on http except of a few pages like login, register,manage etc.):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# force https for /login.php and /register.php
RewriteCond %{HTTPS} =off
RewriteRule ^(login|register)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This exact example should work on my server, but it works only partially, meaning, it redirects all pages to http, but I am unable to open login.php or register.php. The webbrowser states that these pages include redirect loop and they can't show up. By no means I am not an expert on mode_rewrite or htaccess so I would appreciate any help.
edit:
I have followed the suggestions and run mywebsite with chrome plugin. I discovered that in the page login.php has redirect loop http->https->http etc. (The only other redirection on that page is when the user is already signed, but it doesn't seem ike a couse for this loop). I have tried also different codes for setting SSL and this one works:
# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /
# Turn SSL on for payments
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/login\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Turn SSL off everything but payments
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/login\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And this one is working correctly, but it has only one exception for https and I would like to have more of them(around 6). Has someone any idea why the first script is not working? or how to modify the second one in order to have more https websites?
Thanks
I had similar problem, this is what worked for me.
In your httpd.conf, under virtual hosts, make sure you have both:
ServerName domain.com
ServerAlias www.domain.com
BOTH in VirtualHost *:80 AND VirtualHost *:443

Redirect oldsite.com to new.oldsite.com, and sub.oldsite.com to new.oldsite.com/sub/

I have a situation where we've redesigned a site and have moved it to a new domain. The old site has a few client subdomains that we need to redirect to the new domain, but to folders that we will explicitly specify. Note that the new domain is actually a subdomain of the old domain, so not sure how that complicates anything, if at all.
For example, oldsite.com currently redirects all request to new.oldsite.com with the following rule:
RewriteCond %{HTTP_HOST} !^\.oldsite\.com
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
I also want to redirect some specific subdomains to folders of new.oldtsite.com, like subdomain.oldsite.com to new.oldsite.com/subdomain/. There also the likelihood that the old subdomain and the new directory will have different names (subdomain.oldsite.com to new.oldsite.com/subdomain-name/. :-/
I've attempted to duplicate this rule for the subdomain to folder redirect, but that just hijacks all redirects.
I will give you a virtual high-five if you can spot me a fix.
Have your rules from specific to generic. Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/subdomain-name/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
EDIT: To redirect any.oldsite.com/foo to new.oldsite.com/any/foo
RewriteCond %{HTTP_HOST} ^([^.]+)\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/%1/$1 [R=302,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Making sure www. is in browser URL bar

When I enter my website address, it does not always put www. in front of it.
This is because I type domain.com instead of adding the www. in front as this is quicker.
What code do I have to use to make sure that even if I type domain.com it always adds the www. infront.
(I believe this has to do with .htaccess mod_rewrite function?
Thank you,
Chad.
In your .htaccess file, add the following lines (replacing mywebsite.com with your real website):
<IfModule mod_rewrite.c>
RewriteEngine On
#redirects all requests to www.mywebsite.com
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L]
</IfModule>
Note that as google recommands it is better to use a 301 redirection.
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com[nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

.htaccess redirect non-www to www problem

I'm using the following .htaccess file to do few mod-rewrites and also redirect visitors from non-www to www. But the problem is, when someone visit http://domain.com/terms it redirects them to http://www.domain.com/document_terms.php by ignoring the 3rd line.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^terms$ document_terms.php
RewriteRule ^privacy$ document_privacy.php
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
Is there a way to fix this?
You just need to change the order of your rules so the www rewriting comes first.

Resources