lopping .htaccess condition rule when in aws cluster - .htaccess

I am trying to redirect a link ONLY if it's www.domain to a subdomain admin.domain I have been looking around and I am thinking it's my syntax messing me up. I am not so great with .htaccess redirects and rewrites. Here is what I have used so far.
Example 1
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RedirectMatch 301 ^/wp-admin/?(.*)$ http://admin.mydomain.com/wp-admin/$1
Here is the second on I also have been trying.
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^/wp-admin/(.*)$ http://admin.mydomain.com/wp-admin/$1 [QSA,L,R=301]
This is working off an amazon AWS cluster so the code needs to stop once it rewrites only with www. That is where the loop keeps happening.
Any help would be deeply appreciated!

I think you got it backwards, you want to redirect IF the host is www.mydomain.com, not when it ISN'T. So get rid of the !:
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^/?wp-admin/(.*)$ http://admin.mydomain.com/wp-admin/$1 [QSA,L,R=301]
Note that the first code block you have won't work at all. You're using a mod_rewrite condition with a mod_alias directive, they're completely independent of each other.

Related

Subdomain url redirect to a URL

We have about 50 URLs that need to be redirected, but subdomain names may differ, for example:
http://abc.OLDdomain.com > http://cba.NEWdomain.com
http://foo.OLDdomain.com > http://bar.NEWdomainc.com
Most of the answers suggest rules, but in our case source / target subdomains often differ, so it's not a straightforward redirect rule that could solve it.
Also, can you do this type of redirect without rewrite rules?
So ideally something like
redirect 301 abc.OLDdomain.com http://cba.NEWdomain.com
Which does not currently work
I'm asking if there's a way to do it without rewrite rules because the htaccess is managed by someone who's not tech savvy but understands simple redirect directives such as the one above.
My current working solution with rewriterule/rewritecond is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://cba.NEWdomain.com%{REQUEST_URI} [R=302,NC,L,QSA]
RewriteCond %{HTTP_HOST} ^foo.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://bar.NEWdomainc.com%{REQUEST_URI} [R=302,NC,L,QSA]
If there's a one liner alternative without "RewriteCond and RewriteRule" that would be great

301 Redirect from http to https and new page name

I have a site that is going from a non secure HTML/PHP site to a secure Wordpress site. All pages are changing. Also going from www to non-www.
So as an example
http://www.sitename.com/contact.php
will become
https://sitename.com/contact-us/
I know how to do a typical redirect in .htaccess:
Redirect 301 /oldpage.html http://www.example.com/newpage.html
But I'm not sure how to do this when both the page location and the HTTP/HTTPS is changing as well as the www going away. The site ranks pretty well and I don't want to lose any of that.
I've researched but can't find an example of doing both at the same time.
You should be able to do using rule like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^contact\.php$ https://%1/contact-us/ [R=301,L,NC]
Since your page names are different you will need multiple rules like this.
Make sure to keep these rules above all other rules.
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^contact\.php$ https://example.com/contact-us [NC,L,R=301,NE]
The rule above rewrites the following requests :
http://www.example.com/contact.php
or
http://example.com/contact.php
or
https://www.example.com/contact.php
to
https://example.com/contact-us

.htaccess rewrite domainnames, save subdomain and parameters

i have 2 domains but want use only one.
these some examples of domains...
x://test1.domain1.xx/parameter/1
x://test1.domain1.xx/parameter/2
x://test1.domain1.xx/parameter/3
x://test2.domain1.xx/parameter/1
x://test2.domain1.xx/parameter/2
x://test2.domain1.xx/parameter/3
x://test1.domain2.xx/parameter/1
x://test1.domain2.xx/parameter/2
x://test1.domain2.xx/parameter/3
x://test2.domain2.xx/parameter/1
x://test2.domain2.xx/parameter/2
x://test2.domain2.xx/parameter/3
now i want only use xx.domain2.xx/params... all sub/domains with maindomain "domain1" should be rewritten or redirect. without loose parameters or subdomain.
thanks a lot.
I tried sometimes an i think i got a solution.
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain1.xx [NC]
RewriteRule (.*) http://%2.domain2.xx/$1 [R=301,L]

.htaccess redirect folder to subdomain

I've tried applying a few of the answers found on stackoverflow, but either I'm missing something or I'm plain dumb.
Basically I got a main domain name. This domain already has a non-www redirect. So http://domain.com becomes http://www.domain.com. This domain also has a mobile version found inside the the 'm' folder. So accessing the domain name like http://www.domain.com/m/ works and so does http://m.domain.com. What I'm trying to achieve is simple: anyone whom goes to the site via http://www.domain.com/m/, or http://www.domain.com/m/about should be redirected to the subdomain version so to http://m.domain.com or http://m.domain.com/about in the second case listed above.
Whatever I tried implementing ended up with errors, either I managed to disable direct access to m.domain.com, but it worked via domain.com/m/, or redirect loops.
Thanks!
You can use this code in your DOCUMENT_ROOT/.htaccess file of domain.com main .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^m/(.*)$ http://m.%1/$1 [L,NC,R=302]
# non-www to www
RewriteCond %{HTTP_HOST} !^(m|www)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,NC,R=302]

htaccess rewrite rule for subdomain results to redirect loop

Below is my htaccess config
RewriteEngine On
# if the domain starts with "apply."
# i.e. apply.domain.com
# redirect to application URI
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteRule !^formwizard /formwizard/apply [R=301,L,NC]
I have tried it, and when I go to http://apply.domain.com/ it successfully redirects to http://apply.domain.com/formwizard/apply.
My problem is, once it redirects to that URI, it goes to a redirect loop.
Can anyone please help hit me what is wrong with my config?
Background:
I have 3 subdomains: www.domain.com, apply.domain.com, and admin.domain.com. It all references to the same source codes and just managed by my htaccess.
Once the person goes to apply.domain.com, it should be redirected to the application page which is /formwizard/apply.
PS. I don't want to depend too much on the backend codes because I believe this is a server config problem.
Thanks in advance!
If there are more rules then this rule might be getting impacted by them. Modify this rule to this:
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteCond %{THE_REQUEST} !/formwizard/ [NC]
RewriteRule ^ /formwizard/apply [R=301,L,NC]
Also make sure this is very first rule below RewriteEngine On line.

Resources