Simple http (including www) to https htaccess - .htaccess

I want to verifiy that if my redirection code is implemeted good to dont have problems in seo.
Currently using this code:
I want to do this:
- http://mydomain.tld to https://www.mydomain.tld
- http://www.mydomain.tld to https://www.mydomain.tld
My currently code is:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Currently is redirecting but I dont know if 301 is implemented good?

If you're concerned if a redirect is having a negative impact on organic rank, such as multiple redirects (rather than one clean redirect), you can use this tool to check each redirect 'hop':
RedirectDetective.com

Related

Force HTTP for all pages except specific URLs

I have a situation where I need to force every single page in my site to redirect to HTTP except for two specific URLs which need to force redirect to HTTPS.
The two pages that need to redirect to HTTPS pages are:
/microsoft-moc-on-demand-video-training/moc-registration-page/
/courses/register/
The code I've been using in my .htaccess file looks like this:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(/courses/register/|/microsoft-moc-on-demand-video-training/moc-registration-page/)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Unfortunately this doesn't seem to be working. The entire site does redirect to HTTP (so part of the code works), but those two exceptions (which should redirect to HTTPS) do not do that, they stay as HTTP links.
Any idea what I'm doing wrong here?
The problem is that for RewriteRule, there is no initial / in the requested path. Therefore, you're trying to match something that isn't present.
You also had an extra / at the end of each option in the first capture group, which when combined with the final / would require a path such as /courses/register//.
The following code should suit your needs:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(courses/register|microsoft-moc-on-demand-video-training/moc-registration-page)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

htaccess rewrite conditions depending of variables for mobile sites

Hi i have a site with drupal and i use a module for make redirection depending of the agent, if mobile redirect to http://m.url.com but if no, stay in desktop site, but i want to redirect always to http://www.url.com if you write url.com for this, i use this
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
but this no works for me, because i use a module to redirect to mobile site, then, if use the code before always redirect to www.url.com but in mobile also, redirect to www.m.url.com and no working for mobile site, please how to make a exception in the code before, always redirect to www.url.com except m.url.com
thanks
Solution
with the answer before, i resolve for this way
RewriteCond %{HTTP_HOST} ^url.com
RewriteCond %{HTTP_HOST} !^m.url.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You can use:
RewriteCond %{HTTP_HOST} !^(?:www|m)\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect oldsite.com/index.html to newsite.com/index

I know that this shouldn't be too hard to do
I wanted to know how to redirect:
oldsite.com/guide.html to newsite.com/guide
I was playing around with things, but oldsite.com/guide.html was redirecting me to newsite.com/index.php?route=guide.html, not newsite.com/guide
I use opencart in this two site.
I use this code in my oldsite:
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]

complex mod_rewrite. Any idea?

I want to :
- switch from http to https if http is used
- redirect the subdomain to index?o=subdomain except www
- redirection the subdirectory to index?u=user
Example :
http://www.mydomain.com will be redirected to https://www.mydomain.com
http://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
https://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
http://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
https://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
Is mod_Rewrite the best to do that ? Any idea ?
Thanks in advance
I don't have time to test it right now, but you can try this and see if it works. There may be some potential for some things to go wrong, so if you have trouble with it I'd be happy to work out any kinks later. Also, I think that I covered everything you wanted to do, but let me know if I left something out.
RewriteEngine On
# Force redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}/$0 [R=301,L]
Edit: I've updated the ruleset below. I thought about your question though, and aren't you going to have issues attempting to serve up your subdomains over TLS/SSL? That aside, one of the following should do what you want (without errors this time, I hope):
If you wanted internal redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ /index?o=%1%2
If you wanted external redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ https://www.mydomain.com/index?o=%1%2 [R=301,L]

Using htaccess to redirect requests from non-www to www (but with a twist)

I'm currently using htaccess to force all requests to use www. So:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
to redirect example.com/page to www.example.com/page.
However, this htaccess file is being used by several international versions of example.com. What code do I want that can also do the following:
example.de/page -> www.example.de/page
and
example.co.uk/page -> www.example.co.uk/page
etc.
Potentially, there could be dozens of versions of example.com, so I'm looking to avoid having to remember to edit htaccess every time we add a new country.
You can use server variables in your substition, so this should be doable with
RewriteRule ^(.*)$ http://www.${HTTP_HOST}/$1 [L,R=301]
Right: think I've actually got it cracked. (Or at least it looks like it works):
RewriteCond %{HTTP_HOST} ^example\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example\.%1/$1 [R=301,L]
seems to do the trick.
Test if the current host name does not begin with www. and add it if missing:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Resources