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]
Related
This won't work, I don't know why...
# let's say I'm in HTACCESS of domain www.aaa.com
RewriteRule subfolder/script\.php\?param=value¶m2=value2 http://www.bbb.ccom/otherscript.php?otherparam=othervalue [QSA,L]
# domain bbb.com is mine, BUT not on the same account (but same server)
It's not causing any bug or loop, it simply plain not work.
Other instructions in the HTACCESS does work (rewriterules among them) so I know the RewriteEngine is on and works.
It simply won't work because RewriteRule doesn't match QUERY_STRING. You will RewriteCond for that. Use code like below:
RewriteCond %{QUERY_STRING} param=value¶m2=value2 [NC]
RewriteRule ^subfolder/script\.php$ http://www.bbb.ccom/otherscript.php?otherparam=othervalue [QSA,L,R]
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.
I have multiple domains on my server. I want to redirect all of them to one (example.net).
My .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
I’m redirecting all URLs on my server to one main domain, but that domain is also redirecting to itself. So www.example.net returns 301 Moved Permanently and redirects back to itself. I’m told that this isn’t good for SEO. How could I fix this?
You need to add a Rewritecond to prevent it from redirecting when you’re already on the domain that you want. There are loads of examples online if you google it, or see the RewriteCond section of Apache’s mod_rewrite documentation.
What you’re looking for is something like:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
Just small note:
Thanks goes to TRiG, but I had to remove one slash to make it work correctly (coz it added two slashes after domain name). This works for me:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net$1 [R=301,L]
I have checked previous questions, I believe this is quite simple but I can't seem to work it out.
In my .htaccess file I currently have
RewriteCond %{HTTP_HOST} !^www\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
However this is causing a problem as it also redirecting any sub-domains (specifically au.bodycleansediet.com and ca.bodycleansediet.com) to www.bodycleansediet.com
I want them NOT be to be redirected so they can be viewed on their sub-domains.
I know I need to add an exception/re-write rule but I am not sure how to construct it.
Any advice on how to construct this?
Should not something along the following lines work
RewriteCond %{HTTP_HOST} !^(www|ca|au)\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
Since you are basically checking if not (www OR ca OR au) then do your redirect.
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]