htaccess subdomain redirect in infinite loop - apache

I have a domain and also a subdomain which points via dns to the domain.
We want request at subdomain.domain.com to point to /a/b/page while keeping the subdomain.
eg: subdomain.domain.com goes to subdomain.domain.com/a/b/page
It currently is in an infinite loop.
RewriteCond %{HTTP_HOST} subdomain.mydomain.com
RewriteCond %{REQUEST_URI} !^/a/b/page$
RewriteRule (.*) http://subdomain.mydomain.com/a/b/page [R=301,L]
We are using apache2 and modx as the cms.

According to your example:
subdomain.domain.com
goes to (Redirects to, is rewritten as)
subdomain.domain.com/a/b/page
RewriteCond %{HTTP_HOST} ^subdomain.*$
RewriteRule (.*) http://subdomain.mydomain.com/a/b/page [R=301,L]
There is no need to check the URI as there is NONE.

Related

Redirect only HTTP subdomain to HTTPS subdomain in htaccess

How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the .htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .htaccess file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess and simply remove the condition that checks against HTTP_HOST. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
Please try one of the followings:
you need to edit
Redirect "/" "https://yourwebsite.com/"
OR
you dont need to edit following
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

Redirecting all subdomains to a domain

I am having an issue with one of my sites where I need to redirect all subdomains to the main domain name. I have a Drupal site set up that shares content between 2 domains (using the Domain module). Here is basically what is going on.
In my .htaccess file I have these rules..
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
This redirects any subdomain to mydomain.com, which is great!
The problem is that I have another domain (myotherdomain.com) which uses the same Drupal site to share content with via the Domain module.
With that .htaccess rule in place when I go to myotherdomain.com it is redirecting to mydomain.com which I do not want to happen. Is there any way that I can stop that from happening?
To recap:
anything.mydomain.com needs to redirect to mydomain.com
any traffic to myotherdomain.com needs to stay at myotherdomain.com and not get redirected.
Any RewriteCond before a RewriteRule will be applied. Have you tried adding simply RewriteCond %{HTTP_HOST} !^myotherdomain\.com$ [NC]?
Just add another exclusion condition for your other domain:
RewriteCond %{HTTP_HOST} !^myotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
This way, any request for "myotherdomain.com" won't get redirect, and any request for "mydomain.com" won't get redirected.

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.

.htaccess redirect from subdomain not passing subdomain part

I have an .htaccess file which catches all subdomains (I am using a wildcard DNS record) and redirects them to a secure login page, but only if the subdomain isn't www. My code for this is:
# Turn rewriting on
RewriteEngine On
# If no subdomain is supplied then add www by default
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
# If the subdomain isn't www then redirect to the login page
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://secure.mydomain.com/a/%1 [R=301,L]
This is working in part. The catching of the www and inserting if it's not there is fine, but what it's not doing is if I type:
http://sample.mydomain.com
I want:
https://secure.mydomain.com/a/sample
but the subdomain bit isn't being appended to the end. I thought the %1 bit would do this but it seems to not be working.
Also, as a second thought, how can I catch:
https://secure.mydomain.com
where nothing is after .com and redirect it back to:
http://www.mydomain.com
Anybody any thoughts?
Do something like that:
# If the subdomain isn't www then redirect to the login page
RewriteCond "%{HTTP_HOST}" !^www.* [NC]
RewriteCond "%{HTTP_HOST}" ^([^\.]+).*$
RewriteRule ^(.*)$ https://secure.mydomain.com/a/%1 [R=301,L]
In general you have to catch the subdomain part and backreference it. So I added a second RewriteCond that does just that. Backreferences in RewriteConds are accessed with '%n' patterns in RewriteRules.

Are these two re-writes the same?

How are the following different? Ignore the domain names.
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com
RewriteRule (.*) http://www.yourdomain.com/$1 [L,R=301]
The difference is "^"?
What I basically want to do is have my site as http://yourdomain.com and never have the www appear. For a start its shorter and its good for SEO as my site won't be judged as two sites. One with www and one without.
Thanks all
No, they are not the same.
The first says, redirect to the host example.com if the host is www.example.com.
The second says, redirect to www.example.com if the host is not www.example.com.
And even if you would rewrite the second to the following (having both rules redirecting to example.com:
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule (.*) http://example.com/$1 [L,R=301]
So that it would redirect to example.com if the host is not example.com. The result might be the same if the host can only be www.example.com and example.com. But if it can have more values than that (e.g. foobar.example.com), the your first rule would not redirect while my would redirect.