I currently use the following code for my htaccess file. It redirects to https://www.example.com
#if not forum.example.com and not ssl then redirect
RewriteCond %{HTTP_HOST} !^forum\.
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]
#redirect if https://forum.example.com
RewriteCond %{HTTP_HOST} ^forum\.
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://forum.%{HTTP_HOST}/$1 [R,L]
The problem I have is when I go to
http://www.example.com
, this code makes it redirect to
https://www.www.example.com
instead of
https://www.example.com.
Anyone know how I can fix this?
If you require additional information to assist, just let me know.
The first rewrite rule is executed if 2 negative conditions are met: Not forum and not SSL, but the rewrite inserts www always, even when it is already in the URL. It is not previously checked.
I think the best way to correct the problem without modifying the actual rules, except removing the www from the first substitution, is by adding another rule at the begining, like this:
#non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com/?$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#if not forum.example.com and not ssl then redirect
RewriteCond %{HTTP_HOST} !^forum\.
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
#redirect if https://forum.example.com
RewriteCond %{HTTP_HOST} ^forum\.
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://forum.%{HTTP_HOST}/$1 [R,L]
What the added rule does, is to rewrite any URL without www to one with www. The resulting URL is then presented to the other rules.
I don't know if these 2 last rules work as intended, I guess they do. I am just trying to solve the www duplication issue in the question.
Hope this helps.
Related
I try to solve folling problem with .htaccess.
I use it to rewrite everything to "https://" and put "www" in front of every url.
Now I want to use a SSL-certificate. To validate it, I put a html-file in a certain folder. I do not want this to be redirected to "www.". How can I create an exception only for this one file?
Thank you very much for helping me with this maybe kind of stupid question.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
You did not provide an example of URL that should not be redirected to https. But here is an example. This is a simplified version of your .htaccess file.
I have combined the https and www redirect in one directive.
Assume your domain name is example.com.
There is a negative condition (note the exclamation mark). If the request URI does not begin with index.html then the rule fails and is not enforced.
So http://example.com/index.html will not be converted to https, but there is one important caveat: in this case the www. will not be added either. I understand this what you want, then we can have a simplified set or rules.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
This is driving me bananas. For some reason my domain will not work to redirect https-WWW to https-non-WWW. This works with every other permutation except https://www.nextoronto.com
What code do I use in the .htaccess that will allow it to work for all permutations?
Edit: This is the rewrite code now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.nextoronto\.com [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [L,R=301]
It looks sound, perhaps that rule isn't being reached due to other rules in front of it?
Here is what I use that works:
# Force SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# My other rewrites for routing all requests to index.php go here...
Edit the URL and try with this code:
RewriteEngine on
rewritecond %{http_host} ^www\.example\.com [nc]
rewriteCond %{SERVER_PORT} 80
rewriterule ^(.*)$ https://example.com/$1 [r=301,nc]
Step 1
You should make .htaccess file like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1
Step 2
Go to the directory, then view mark the option file extension. Then see the .htaccess file.
When no rewrite works, no matter what you do, consider what happened to me. IIS or WAMP, on my laptop, redirected all calls to my domain (call it example.com) to localhost. So creating all the rewrites on my example.com server were to no avail because no calls to example.com ever made it out of my computer. Just do a "ping example.com" and if you get an IP of 127.0.0.1, edit your HOSTS file.
I went the entire website to be accesed only with https.
In .htaccess to i written the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.my-website.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.my-website.com/$1 [R,L]
But it causes "This webpage has a redirect loop"
Anyone can guide me a fix for this.
Normally with a single site, you can use that:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.my-website.com%{REQUEST_URI} [R=301,L]
I am using the following condition/rule on a .htaccess file located at the root of my domain. The purpose is to redirect all non-www requests to their www. version:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
It seems to be working fine.
Inside my /blog/ subdirectory I have another .htaccess that I use to redirect fancy URLs to real ones:
RewriteCond %{THE_REQUEST} !.*?url=.*
RewriteRule (.*) index.php?url=$1 [QSA]
This also seems to be working fine. However, all non-www requests inside the /blog/ subdirectory are not being redirected to their www. version.
For example, if I type domain.com on the browser I correctly get redirected to www.domain.com. But if I type domain.com/blog/ or domain.com/blog/test-page/ I won't get redirected to the www. version.
Probably the .htaccess inside /blog/ is conflicting with the one at root level, but I don't know how or how to fix it. Any clues?
Update: I solved the problem by putting all the rules on the root .htaccess file. I had to tweak the fancy URL rules slightly to only catch the /blog/... requests. Here's the final .htaccess file in case it might help you:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^apprush\.com$ [NC]
RewriteRule ^(.*)$ http://www.apprush.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/blog
RewriteCond %{REQUEST_URI} !.*?url=.*
RewriteRule blog/(.*) blog/index.php?url=$1 [QSA,L]
Are you able to put all of the rewrite rules into the root level .htaccess? It not only gets around the problem you're having but is a little neater because you know exactly where all of your rules are located.
If you are, these rules will do what you need:
RewriteCond ${HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond ${THE_REQUEST} !/blog/.*?url=.*
RewriteRule (.*) index.php?url=$1
This is because you have dollar sign $ at the end of domain.com. Also you need to use REQUEST_URI instead of HTTP_HOST. Remove it:
RewriteCond %{REQUEST_URI} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
The dollar sign signifies the end of the string, thus making the rewrite to work for direct requests to domain.com. The update script solves the problem.
I have a certain sub-domain which requires an SSL connection for all pages on that subdomain.
This is my current .htaccess script:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
I have a single page on the subdomain which must not have SSL access (causes issues with accessing files on another server without a certificate).
My mod_rewrite knowledge is very limited. I've done a search but can't find what I need.
My question is, the page which must not have https is called 'tutorial.php'. Is there a way to redirect all pages except tutorial.php to https?
I guess in pseudo-code, the RewriteCond would be similar to:
RewriteCond %{SERVER_PORT} 80 AND {WEB_PAGE}!='tutorial.php'
Thanks
Try this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/tutorial\.php
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteRule ^tutorial\.php$ http://%{HTTP_HOST}/tutorial.php [R,L]