I hope asking a new question is the right approach to follow-up to an existing question(?) - please bear with me if not, I'm pretty much new to stackoverflow. That said, I'm referring to hide html extension + redirect .html version + special case exception
The htaccess provided by user Jon Lin works perfectly fine so far, but now I'm struggling with a related problem (I think it's related to the htaccess) and that is to use a shared ssl certificate for just one specific page on our site, let's call it baz.html
Due to the existing/cited htaccess, hxxp://mydomain.com/baz.html resolves to hxxp://mydomain.com/baz, which is fine. But, trying to use a shared ssl at hxxps://shared.host.com/~user/baz(.html) leads to a 404 not found error. I'm pretty sure (I assume so) that this is due to the used htaccess as the shared ssl tutorial provided by our host is pretty much unmistakeable.
Also, the document root (foo.html) shows up (as the only not-404) when accessing it with shared ssl but the CSS(+images), although relatively linked within the html code, isn't processed. I assume(?) this is related.
Basically, what I'd like to achieve is linking hxxps://shared.host.com/~user/baz instead of hxxp://mydomain.com/baz within our main site navigation to achieve a secured page (same as I do now for foo(.html), bar(.html), just for unsecured content)
I hope that my actual problem is perfectly understandable(?). I do not want to link our actual project page to not do self-advertising of some sort. If access to the actual page is needed to answer the question I'll certainly provide a link (if explicitly asked for)
Thanks in advance.
You can't use the same htaccess file in your shared SSL host because the base is different (/~user/ instead of just /), and that will change how all the rules inherently work. So for those same rules, you need to account for the different base. If you put these rules in the htaccess file in the /~user/ directory, they'd need to look like this:
EDIT: since they're the same document root, you need to duplicate all the rules, one for SSL, one not:
RewriteEngine On
# 1. hiding the .html extensions
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/~user/%1.html -f
RewriteRule ^ /~user/%1.html [L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^ /%1.html [L]
# 2. 301 redirecting .html version
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~user/([^\ ]+)\.html
RewriteCond %1 !foo$
RewriteRule ^ /~user/%1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.html
RewriteCond %1 !foo$
RewriteRule ^ /%1 [L,R=301]
# 3. typing /foo.html to resolve to / as well as typing /foo to resolve to /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/~user/(.*?/?)foo(\.html)?$
RewriteRule ^ /~user/%1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(.*?/?)foo(\.html)?$
RewriteRule ^ /%1 [L,R=301]
Now for the /baz and /baz.html to redirect to SSL, you'd need to add this before any of the rules (right under RewriteEngine On) in your domain.com document root (the non-SSL one):
EDIT: since the document root is the same, you need to add the condition to check whether it's SSL or not
RewriteCond %{HTTPS} off
RewriteRule ^/?baz(\.html)?/?$ http://shared.host.com/~user/baz [L,R=301]
Related
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 recently added an SSL to my sites. I have added the code to the .htaccess file to force the https. The issue is that my external links that go to pages within the site are now being redirected to the homepage. The code I am using is:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonelec.com%1 [R,L]
I think the issue is in the last line, as the rule is telling it to redirect to the homepage. What I can't seem to find is a rule that will say for it to go to the URL provided in the link but give it an https instead of the HTTP.
I did do a search for this topic, but all the code I found was similar to what I already had. Thank you for all your help.
Update
I have two sites I am trying to work this out for, watsonenerysolutions.com and watsonelec.com.
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonenergysolutions.com/$1 [R,L]
It still sent to the homepage
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^ https://www.watsonenergysolution.com%{REQUEST_URI} [R,L]
I received an error message that said Safari can't open the page "https://www.watsonenergysolutions.com/index.php" because Safari can't find server "www.watsonenergysolutions.com"
%N backreferences are what you match in RewriteCond's. In your case, it is empty. That's why anything is going to the homepage.
You need to use $1 or %{REQUEST_URI}, both rules below are equivalent (the second may be faster because you don't -re-match unnecessarily)
RewriteRule ^(.*)$ https://www.watsonelec.com/$1 [R,L]
RewriteRule ^ https://www.watsonelec.com%{REQUEST_URI} [R,L]
Note 1: %{REQUEST_URI} value always begins with a leading /, while what you can match in a RewriteRule never begins with a leading /
Note 2: R flag uses a 302 redirect by default. Maybe you'll want to use a 301 ([R=301,L])
I hope that I'll explain myself in my needs. I'm trying to write a dynamic htaccess but I don't achive to understand the conditions and the rewriting rules.
Here is my thing, I built a small CMS where the user will write their base url like "stackoverflow.com" and he'll choose with some radio input between http or https and also between www or not. Also for SEO porposes, one thing that it's important is to force the slash except in url with extensions.
So, the options for ssl and www are stored in DB, so I wanted to know what do you think, if it's better to write some code like:
if($ssl){
$htaccess.= "RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1/ [R=301]\n";
}else{
$htaccess.= "RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301]\n";
}
As you can see that works if I want to add the www, so I was trying to add more php conditions but i'm sure there must be a way to do it in just a few htacess lines, I already see that we can use conditions like this:
RewriteCond %{HTTP_HOST} ^www.stackoverflow.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s)
RewriteRule ^ http%1://www.stackoverflow.com:2368%{REQUEST_URI} [P,QSA,L]
But again all this codes I found have a fixed www or non-www and I don't know if they keep the slash. So, I want to know if I can add more conditions to the code above to have the www as variable to, and to ask if there's some extention to erase the slash. Something like:
RewriteCond %{HTTP_HOST} ^www.stackoverflow.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s)
RewriteCond %{HTTP_HOST} !^www. [OR]
RewriteCond %{HTTPS}:s on:(s)
RewriteRule ^ http%1://%2stackoverflow.com%{REQUEST_URI} [P,QSA,L]
I wrote this but I don't know if I can keep the variable (s) with the same name, or if %2 take the value, or if there's a limit of htaccess conditios. Because I was trying to found some htaccess docs for this but I don't get it, and everything I try make show some wrong configuration screen so I hope that you can at least give some advices about how to use this variables or if you think If there's and easy way to do it.
Thanks in advance.
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'm having some trouble with my .htaccess redirections.
I want a situation in which the (non-www)domain.tld is redirected to the www.domain.tld. And I want to rewrite the arguments to skip the index.php, making a request for /foo go to index.php/foo.
Initial situation
First I had these rules
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\ /:-]*)$ index.php [L]
And this worked. Mostly. What didn't work was that in PHP $_SERVER['PATH_INFO'] stayed empty and I disliked the whitelisting of the characters.
Change for PATH_INFO and to accept more
So I changed the last line into this:
RewriteRule ^(.*)$ index.php/$1 [L]
This fixed the PATH_INFO and the limited characters. However, I recently noticed that this caused the non-www redirect to www. to fail miserably.. When going to the non-www domain Apache says
Moved Permanently
The document has moved here.
Where 'here' is linked to the same thing I typed (non-www domain.tld) and thus failing to serve the user.
Continuing the search..
I found a lot of Q&A here and elsewhere or the topic of non-www redirections, but all seem to fail in some way. For example:
RewriteCond %{HTTP_HOST} !^www.*$ [NC]
RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
This just didn't do that much. Nothing got redirected, although the website was served on the non-www.
Anyone knowing what I do wrong or having a solution for this mess? :)
(Preferably, I would like the non-www redirection to be global. So that I don't have to change the actual domain name every time.)
I guess you’re just missing the L flag to end the rewriting process:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And make sure to put this rule in front of those rules that just cause an internal rewrite.