How can I create an exception in .htaccess? - .htaccess

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]

Related

How to redirect to subdomain in .htaccess file with multiple domains

I have an .htaccess file that sends example1.com, example2.com, and example3.com to different files on my server
I want to make it so example3.com temporarily redirects to a subdomain that has been given its own A record: forums.example3.com. Would like to do it in .htaccess instead of DNS settings. It should show forums.example3.com in the URL bar too. I want to also make it so I can remove the redirect in the future without too much re-coding. So far i cant even figure out the redirect as every attempt at rewriting this working code for the redirect always fails:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+.)example2.com [NC]
RewriteCond %{HTTP_HOST} !^example2.com$
RewriteRule ^(.)$ http://example2.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+.)example3.com [NC]
RewriteCond %{HTTP_HOST} !^example3.com$
RewriteRule ^(.)$ http://example3.com/$1 [R=301,L]
#othersites in othersites folder
RewriteCond $1 !^othersites/
RewriteCond %{HTTP_HOST} ^example2.com$
RewriteRule ^(.)$ /othersites/example2.com/$1 [L]
RewriteCond $1 !^othersites/
RewriteCond %{HTTP_HOST} ^example3.com$
RewriteRule ^(.)$ /othersites/example3.com/$1 [L]
what code should I add/change to redirect example3.com to forums.example3.com? Preferably in the most SEO friendly way.
First of all, this rule is most likely wrong: RewriteRule ^(.)$
One single dot represents one single character, whereas you usually want to allow zero or more.
What you want instead should be RewriteRule ^(.*)$
The exclamation mark specifies a negative match. So if the host being requested is example3.com, then this rule will be met:
RewriteCond %{HTTP_HOST} !^example.com$
But the previous one would fail anyway:
RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
However I would write more explicit rules and avoid negative matches if unnecessary, as they can easily lead to unwanted results.
So, the following rule should be enough to redirect example3.com (with or without subdomain) to http://forums.example3.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} (.+\.)?example3.com$ [NC]
RewriteRule ^(.)$ http://forums.example3.com/$1 [R=302,L]
Since you are saying you want to set up a temporary redirect, you may instead use HTTP code 302 for the redirect.

.htaccess mod_rewrite redirection not working

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.

.htaccess redirect code

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.

Non WWW to WWW redirection using htaccess File

I am using Codeigniter-.
My domain currently does not redirect to www version.
For example if I type mydomain.com then it stays mydomain.com. I want to redirect it to www.mydomain.com.
If someone types mydomain.com/controller/method then it should be www.mydomain.com/controller/method.
Another problem: I already tried other solutions but the problem is when it redirects to www version, it automatically adds "index.php" in the URL. But when I type www in the domain name then it works fine, no "index.php" in the URL. This problem occurs only during the redirection.
Here is my .htaccess file (I've removed the redirection code)
RewriteCond $1 !^(index\.php|system|rpc_relay.html|canvas.html|robots\.txt)
RewriteCond $1 !^(sitemap\.xml|export)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any help would be greatly appreciated.
To redirect from http:// to http://www. , and also remove the route file (index.php) in the url, put these lines on your htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|images|css|js|styles|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
the domain is :
domain.com
folder with direct access :
images|css|js|styles
hope this help
I've used the following before:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
To redirect domain.com to www.domain.com, you could use the following rewrite rule. Please replace domain.com with your own domain name.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I don't know why there are such complex RewriteRules answers, even though Gobhi has provided a nice generic solution (= whatever the domain name is, it works).
Here's my solution.
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule (.*)/index\.php$ $1/ [QSA]
</IfModule>

.htaccess mod_rewrite redirection between domains

I have two domains, cshen.ca and cshen.net, both point to the same place.
I'm trying to accomplish the following with mod_rewrite:
cshen.net is 301 redirected to cshen.ca
www.cshen.net or www.cshen.ca are both redirected to cshen.ca
the path after the domain is preserved after being redirected, for example www.cshen.net/foo/bar/ would be redirected to cshen.ca/foo/bar/
I've tried a variety of rules, but can't seem to get it to work.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://cshen.ca/$1 [R=301,L]
This accomplishes the first two rules, but redirects everything back to the home page, and does not preserve the rest of the URL.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/(.*)$ http://cshen.ca/$1 [R=301,L]
Making a small change and adding a slash '/' to the RewriteRule preserves the rest of the URL, but only www.cshen.ca is being redirected. Neither cshen.net nor www.cshen.net are being redirected anywhere.
I've also tried Apache's guide and used this code:
RewriteCond %{HTTP_HOST} !^cshen\.ca [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://cshen.ca/$1 [L,R]
I thought this would work, since it should redirect any url that isn't cshen.ca, however, like the second piece of code, it does nothing to cshen.net or www.cshen.net.
I've just about ran out of ideas of other things to try. I would appreciate it if someone could help out!
Addendum: in case it matters, I'm using WordPress's pretty URLs, and the rewrite rules for that are:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried putting my rules before and after the WordPress rules (when I put it before I'd of course add RewriteEngine On before my rules), doesn't make any difference.
Thanks!
don't know how to edit, solved by placing the code from apache before WP's
must have forgotten to test this earlier.

Resources