I have domain-a.com and domain-b.com. The host runs a multi-site Contao installation with two sites to which both domains are assigned respectively. Both sites are supposed to have a Wordpress blog in a /blog subfolder. Of course they realistically can not, so the first one is domain-a.com/blog and the second is domain-b.com/blog-b.
Wrapping my head around .htaccess has proven to be really difficult for me and I just can't figure out how to get this logic to work:
if
domain is domain-b
and
request_uri starts with /blog
rewrite to domain-b/blog-b/$1
I tried like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain-b\.comt [NC]
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^/(.*) /blog-b/$1
Does not work. How is it done?
Ah, I think the $1 is capturing the /blog/ in the incoming url, so it's doing /blog-b/blog/...
Try this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain-b\.com [NC]
RewriteRule ^/blog/(.*)$ /blog-b/$1 [NC,L]
Also, depending if you have a RewriteBase, the leading slash in the RewriteRule may need to be removed.
Related
I have some htaccess that looks like:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?joshrodg\.com$ [NC]
RewriteRule ^ https://joshrodgers.com/$1 [R=301,L,NE]
The first three lines are forcing all of my sites to be https, so we're all good there. The last two lines, I would like to take the non-www and the www of joshrodg.com and redirect to https://joshrodgers.com/.
My code is working, the thing is, it's working a little too good. I use other paths from joshrodg.com like joshrodg.com/example or joshrodg.com/test but I don't want these paths to redirect. I just want the top-level domain to redirect. I'm thinking this is just a quick tweak and I've tried several pieces of code, but am still getting redirected.
Any ideas?
Thanks,
Josh
With "just the top level domain" you actually mean requests to the root folder or to the path "/" only. So you need to match exactly that path only:
RewriteRule ^/?$ https://joshrodgers.com/ [R=301,L]
this is kinda an odd one:
I need my site to do two things (one of which is already working):
if a user tried to access the domain via HTTP:// it is replaced with https:// - this is for SEO in google and to make the user feel more secure -
the site folder that is used to load the website needs to be the subdomain folder of the site
Oddly the second part of this is working and I figured out - however I'm not sure how to merge these two requests:
HTACCSESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^trippy\.co\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.trippy\.co\.nz$
RewriteCond %{REQUEST_URI} !update.trippy.co.nz/
RewriteRule (.*) /update.trippy.co.nz/$1 [L]
But I'm not sure how to make the site display as
https://trippy.co.nz/
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} update\.trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://update.trippy.co.nz/$1 [R,L]
but then the web address displays as: https://update.trippy.co.nz
and I need to remain as https://trippy.co.nz/
Any help here would really great and I know its a odd situation to be in.
THanks,
Wally
...but then the web address displays as: https://update.trippy.co.nz
You would seem to be redirecting to the subdomain itself, not the subdomain's subdirectory, as you appear to be doing in the first rule. You may also be putting the directives in the wrong order - the external redirect needs to go first - otherwise you are going to expose the subdomain's subdirectory, which does not appear to be the intention.
Try the following instead:
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
# Rewrite all requests to the subdomain's subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteRule !^update\.trippy\.co\.nz/ /update.trippy.co.nz%{REQUEST_URI} [L]
No need for the extra condition in the 2nd rule block, as the check can be performed directly in the RewriteRule and use the REQUEST_URI server variable instead of the $1 backreference in the substitution string.
That that R by itself is a temporary (302) redirect. You may want to change that to R=301 (permanent) once you have confirmed this is working OK.
I have a little problem in hands.
I am setting up a domain that as 3 languages, example.com for principal domain, en.(...) for english and de.(...) for germany.
Usually I only redirect the httpdocs from the subdomains to the main with ln -S because all languages executes the same code, the difference is matched from php and mysql.
My new host don't provide any ssh connection so I have to use a different approach on this.
It was told to me that it can be done by .htaccess but I already tried a lot of things but only can redirect, changing the url and that's not possible, it have to keep the same, the contents yes, will be from another domain. Can someone help?
This code should looks like the one you're searching for :
www.domain.en .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/en/$1 [L,R=301]
www.domain.de .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/de/$1 [L,R=301]
You'll propably adapt the http://www.domain.com/lang/$1 part to your needs.
EDIT
Following your comment, this is a code for subdomains redirections :
domain.com .htaccess
RewriteCond %{HTTP_HOST} ^en\.domain\.com [NC]
RewriteRule (.*) http://domain.com/en/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^de\.domain\.com [NC]
RewriteRule (.*) http://domain.com/de/$1 [QSA,L]
Here's what I am trying to do. I have a few scripts on a site I am rewriting the URLs to force them to use https://. What I then want to do, is rewrite urls when I navigate away from the HTTPS pages back to HTTP. Here's what I have now that is not working exactly the way I would like it to.
# For HTTPS pages:
RewriteCond %{HTTPS} off
RewriteRule ^(register/|cms/(.*))$ https://%{HTTP_HOST}/$1 [R=301,L]
# For non-HTTPS pages:
RewriteCond %{HTTPS} on
RewriteRule ^(about/|contact/)$ http://%{HTTP_HOST}/$1 [R=301,L]
The problem I am having is that while the pages like about/ and contact/ rewrite back to HTTP, I can't figure out how to reference the document root, so it unfortunately stays https://. I like using relative urls, so I would rather not go into my source and change everything to absolute if there is a simple htaccess solution.
My question: How do I properly reference the web root in my RewriteRule?
Also, is there a more efficient, catch-all way of doing what I am trying to accomplish that I just don't know about, because I haven't been able to find anyone else with this problem. I am not super-familiar with .htaccess. I learn just enough as I go to do various Rewrite operations, as I will never have a need for the full features, and I find the documentation cumbersome and difficult to follow.
Thanks!
for matching the root use
RewriteCond %{HTTPS} on
RewriteRule ^$ http://%{HTTP_HOST}/$1 [R=301,L]
or combine them with the other rule
RewriteRule ^(about/|contact/|)$ http://%{HTTP_HOST}/$1 [R=301,L]
As to the catch all expect /register and /cms (that's what you mean right?). I think this should do the trick.
# For HTTPS pages:
RewriteCond %{HTTPS} off
RewriteRule ^(register/|cms/(.*))$ https://%{HTTP_HOST}/$1 [R=301,L]
#prevent rules beneath this one in this htaccess file from being applied when url start with register of cms (doesn't do anything else)
RewriteRule ^(register/|cms/) - [L]
#don't force http for resources, to prevent partial encryption errors
RewriteRule ^(css/|images/) - [L]
# For non-HTTPS pages:
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
I've been Googling around for .htaccess redirection information, but nothing I find is quite what I'm looking for.
Basically, I want a solution that will take a site example.com and allow you to enter URL's like:
123.example.com
ksdfkjds.example.com
dsf38jif348.example.com
and this would redirect them to:
example.com/123
example.com/ksdfkjds
example.com/dsf38jif348
So basically accept any subdomain and automatically redirect to a folder on the root of the domain with the name of that subdomain.
Try something like this:
# If we're not on http://example.com
RewriteCond %{HTTP_HOST} .+\.example.com
# Add the host to the front of the URL and chain with the next rule
RewriteRule ^(.*)$ ${HOST}$1 [C,QSA]
# Make the host a directory
RewriteRule ^(.*)\.example\.com(.*)$ http://example.com/$1$2 [QSA]
You don't say what should happen to http://foo.example.com/bar?moo - I've made it go to http://example.com/foo/bar?moo
Change the last line if that's not what you want.
If you just want them to be the entrance:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ http://example.com/%1 [L,R]
Otherwise:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L]