Htaccess Redirect Loop Issue - regex

For particular reasons, I'm hosting a subsite within a parent site, and would like to use a domain alias for the subsite. For clarity, let's call the two domains parent.org and child.org.
The subsite is located at: parent.org/child
Since child.org is a domain alias, it's also located at: child.org/child
I'd like to build redirects that route any requests to either child.org or parent.org/child to child.org/child
Scouring the forums, I've attempted a number of things, though this one seemed the closest to what I'm going for:
RewriteCond %{HTTP_HOST} ^(www.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/.*$ [NC]
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L,NS]
RewriteCond %{HTTP_HOST} ^(www.)?parent\.org/child$ [NC]
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L,NS]
This doesn't quite work as hoped, resulting in child.org/child/child/child/etc.
What am I missing here?
Addendum:
To help clarify my goal here, for either domain, if "/child" isn't the first segment of the URL, I'd like to use parent.org. If "/child" isn't the first segment of the URL, I'd like to use child.org. So:
Parent setup:
parent.org/foo -> parent.org/foo
parent.org/child/foo -> child.org/child/foo
Child setup:
child.org -> child.org/child
child.org/foo -> parent.org/foo
child.org/child/foo -> child.org/child/foo

First RewriteCond %{HTTP_HOST} ^(www.)?parent\.org/child$ [NC] is wrong because %{HTTP_HOST} is representing either parent.org or child.org and could not match uri after them.
And i think you have no issue with child.org because it is already done before by domain alias which means that the request to child.org will show up every thing in child directory unless you do another configuration .
So , your code should looks like this , to redirect only when parent domain is involved :
RewriteCond %{HTTP_HOST} ^(www\.)?parent\.org$ [NC]
RewriteCond %{REQUEST_URI} ^/child/(.*)$ [NC]
RewriteRule ^child/(.*)$ http://child.org/$1 [R=302,L]
So , this part parent.org/child/ is already configured as child.org so any request contains parent.org/child/whatever will be child.org/whatever
Update : you siad Since child.org is a domain alias, it's also located at: child.org/child if that so ,the code should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?parent\.org$ [NC]
RewriteCond %{REQUEST_URI} ^/child/(.*)$ [NC]
RewriteRule ^(.*)$ http://child.org/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^(.*)$ http://parent.org/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L]
So , any request to parent.org/child/whatever will be redirected to child.org/child/whatever
Then any request to child.org/whatever and wherever is everything except child will be redirected to parent.org/whatever
Finally , any request to child.org will be redirected to child.org/child/

Related

How to properly write a RewriteCond for https and subdomains

Thus far I've gotten this far with my RewriteConditions but it's from hacking and slashing from internet examples. They seems to behave a little funny at times, like showing the directory in the url, so I assume I'm not doing it right.
The desired behaviour: I have multiple subdomains all pointing to the same root. Based on the subdomain value I want to redirect to a sub-folder in the root. I also want to force HTTPS. I have multiple subdomains, but I'll only show two for the sake of brevity. All the other conditions are virtually identical.
Here's my .htaccess code:`
RewriteEngine On
RewriteCond %{HTTP_HOST} purchase.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !purchase/
RewriteRule (.*) https://purchase.mydomain.com/purchase/$1 [L]
RewriteCond %{HTTP_HOST} booking.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !booking/
RewriteRule (.*) https://booking.mydomain.com/booking/$1 [L]
`
Thank you,
Mike
Change your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(purchase|booking)\.mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} =purchase.mydomain.com
RewriteRule ^((?!purchase/).*)$ /purchase/$1 [L]
RewriteCond %{HTTP_HOST} =booking.mydomain.com
RewriteRule ^((?!booking/).*)$ /booking/$1 [L]
http:// or https:// in target will redirect instead of rewriting.

htaccess redirect only root domain

I would like to redirect my domain name from one site to another. I only want the rule to be applied if no other subpages are specified
EG:
www.example.com
would get redirected
www.example.com/folder/page.php
would not get redirected
The code I have does a catch all and that is not what I want
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://newsite.com/folder/$1 [L,R=301]
You can use:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteRule ^/?$ http://newsite.com/folder/ [L,R=301]

.htaccess how to redirect subdomain/folder to subdomain only

I'm trying to find an answer on different places, but I can't find the full solution for this.
I had a folder setup for a blog. Now we move to an subdomain setup. The redirect shown below works perfectly. The only thing that doesn't work is when the url is a subdomain combined with folder. that isn't redirected.
RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$
RewriteRule ^blog/(.*)$ http://blog.domain.com/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule ^(.*)$ blog/$1 [L,QSA]
www.domain.com/blog/some-url-here is redirected to
blog.domain.com/some-url-here
When I try blog.domain.com/blog/some-url-here it returns a http-status of 200 and when I try to redirect I get a infinitive loop.
Is there a redirect that I've missed?
Try these rules on top of your other rules:
RewriteCond %{THE_REQUEST} \s/blog/([^\s]*) [NC]
RewriteRule ^ http://blog.domain.com/%1 [L,R=301]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteRule !^blog/ blog%{REQUEST_URI} [L,NC]

mod_rewrite .htaccess subdomain redirect

I spend the last 2 days searching and learning about .htaccess to redirect all subdomains to a user folder
without changing the url, but i keep having 3 little problems. I really would appreciate some help
problem 1)
If i go to http://www.example.com/users/foo it automatically redirect
the url to http://foo.example.com/ but if i go to http://www.example.com/users/foo/dashboard.php
it does nothing, instead of redirecting to http://foo.example.com/dashboard.php
this is the code i use:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /users/(.+)/\ HTTP/
RewriteRule ^users/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
problem 2)
if i go to http://foo.example.com/dashboard.php
it gets its information from http://www.example.com/users/foo/dashboard.php
but if i type foo.example.com in the address bar (in firefox) it (sometimes) automatically redirect
to www.foo.example.com, can i prevent the www prefix?
this is the code
RewriteCond %{REQUEST_URI} !^/users/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule (.*) /users/%1/$1 [L]
problem 3)
the campaign website is locaded in the folder http://www.example.com/website but should be
visible on the url http://www.example.com/ (like i get the information from http://www.example.com/users/foo
on the url http://foo.example.com/).
About problem one I would just check the host:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^users/([^/]+)/(.*)$ http://$1.domain.com/$2 [R=301,L]
About problem second this is a modified rule which I got from the drupal .htaccess file:
# remove www prefix
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com$ [NC]
RewriteRule ^ http://%1%.domain.com{REQUEST_URI} [L,R=301]
# redirect to the subdirectory
RewriteCond %{HTTP_HOST} !^www\.(.+)\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^ user/%1%{REQUEST_URI} [L,R=301]
About your third problem check this one here:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ website/$1 [R=301,L]

Rewrite URL for ANY subdomain with www to corresponding subdomain without www

We have a multi-subdomain site that generates dynamic cotent depending on the subdomain text. However it does not work if www is appended to the subdomain. As some users are used to add www in front of every URL, we would like to fix it with a URL rewrite.
EDIT
I have got this far:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
Surprisingly. It works well in one of my test subdomains, but not in the other:
www.otro-mas.subdominio.dev gets redirected to otro-mas.subdomino.dev (with and without an URI like /index.html). Just as expected.
However www.ono.subdominio.dev is going into an infinite redirect. Like this:
http://www.ono.subdominio.dev/ono.subdominio.dev//ono.subdominio.dev//ono...
Why is it not rewriting the host?
Try this:
RewriteCond %{HTTP_HOST} ^www\.([a-zA-Z-_]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://%1\.domain\.com/$1 [R=301,NC,QSA,L]
You probably forgot the QSA and the L directives (search on the Apache mod_rewrite documentation for the explanation).
It does work.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
I had some problem with the caches in my browser.