I have my .htacces on my server and it looks like so:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
But when I type example.com in my browser it doesn't redirect to https://example.com. How can I do that?
It would seem it was just the spelling of .htaccess (filename) that was the problem. This has two s, not one, as stated in the question.
Bit of an aside... the name of this file (per-directory Apache config filename) can be changed in the server config (although this is rare):
AccessFileName .htaccess
Related
I have the following domain that does not work without the www.
wwww.mydomain.com works
mydomain.com = does not work
The root htaccess file (primary domain is something else) automatically seemed to have this code in it.
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.io\/mydomain" [R=301,L]
I added this code in the website's own htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
What am I doing wrong?
Currently mydomain.com goes to something quite strange (it duplicates the domain name): and has an error
https://www.example.com/example.com/
Error on page
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Can I delete the code which says "do not edit". Which one is working and why/how? A fix with an explanation would be appreciated.
The issue was basically resolved by:
Adding the rewrite engine rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Deleting the forwarding in the DNS management console and ONLY leaving in the forwarding in the addondomains section. So, in the cpanel - addon domains, create forwarding and delete the forwarding in the DNS section. This was the key part.
There was an additional folder outside of the public_html for this domain and that was deleted as well.
Finally, adding these rules to force https was useful:
https://www.hostinger.co.uk/tutorials/ssl/forcing-https
Also noting the helpful comment: "Which web server are you using? Look through your web server configuration files. Your problem is that you have a redirect from HTTP to HTTPS and the path URL is configured wrong."
I have a folder export that is accessible to all my subdomains:
/export/sub1/...
/export/sub2/...
/export/sub3/...
Right now, regardless of what subdomain you're on, you can see all of the content by changing the directory in the url.
It's not a security issue, but more of a canonicalization concern, but I'd like to use an .htaccess file to rewrite the folders so people see a modified path that matches up with their subdomain:
sub1.domain.tld/export/... is served from /export/sub1/...
sub2.domain.tld/export/... is served from /export/sub2/...
sub3.domain.tld/export/... is served from /export/sub3/...
How can I do this?
You can use this generic rule in site root .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+) [NC]
RewriteRule ^/?export/(.*)$ /export/%1/$1 [L,NC]
I would like to redirect all http and https to 1 domain, thus I'd be interested to know the code to add into the .htaccess file in order to redirect:
From: HTTP(non-www) + HTTP(www) + HTTPS(www) To: HTTPS(non-www)
I have found the following answer:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https:// %{HTTP_HOST}%{REQUEST_URI}
but it redirects to HTTPS(www) for as far I understood and mine should be redirected to HTTPS(non-www)
Thanks in advance.
If you always redirect to a specific domain which is always the same, then you just need to put it instead of %{HTTP_HOST} in your RewriteRule.
The %{HTTP_HOST} tells to use the same http hostname as the source of the redirect. But you need to have your own destination name, not copied from the source.
Something like this:
RewriteEngine On
RewriteRule (.*) https://mydomain.com/%{REQUEST_URI}
P.S.: Please make sure the destination domain does not have the same rule to avoid cyclic redirects.
RewriteEngine on
RewriteRule (.+) http://newdomain.com/$1 [R=301,L]
I am using Wordpress and this is my htaccess configuration file, everything seems to be working except this one. This URL:
http://olddomain.com/wp-content/uploads/2012/12/abc-1-thumb.jpg
Redirects to:
http://newdomain.com/403.shtml
Instead of:
http://newdomain.com/wp-content/uploads/2012/12/abc-1-thumb.jpg
Examples:
http://mister-gan.com/wp-content/uploads/2011/02/simple-and-clean-2.png
http://ganchinhock.com/wp-content/uploads/2011/02/simple-and-clean-2.png
May I know why?
RewriteEngine on
RewriteRule (.+) http://newdomain.com/$1 [R=301,L]
Redirect 301 http://olddomain.com/wp-content/uploads/2012/12/abc-1-thumb.jpg http://newdomain.com/403.shtml
Upon #Wige's comment and rephrasing your question it seems that there is a permissions problem in one of the subdirectories on the new domain, as 403 is a permissions error.
Ok, now I am lost.
I am trying to do a simple .htaccess redirect of subdomains to a specific folder on the server, meaning all
subdomain.mywebsite.com
will go to
www.mywebsite.com/s_subdomain
But for some reasons this doesn't work.
I have tried a lot of settings in .htaccess but for no good. Now in my .htaccess I have:
RewriteEngine on
Options +FollowSymLinks
Options +SymlinksIfOwnerMatch
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\.mywebsite\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mywebsite\.com
RewriteRule (.*) /s_%1/$1 [L]
Are there any other settings, or is somethig I have missed?
PS. - I don't have access to http.conf. I have to do it using only .htaccess
Thanks!
This is just a "plain" rewrite (the browser won't see it). To redirect, add the R flag to your RewriteRule.
RewriteRule (.*) /s_%1/$1 [L,R]
The rest seems right, although I haven't tested it. For debugging you could consider RewriteLog, see http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog
So, neither solution does work? Try something simple then.
RewriteEngine on
RewriteCond ${SERVER_NAME} ^(subdomain)\.yoursite\.com$ [nc]
RewriteRule ^(.*)$ http://www.yoursite.com/s_%1/$1 [L,R]
To test if your subdomain is handled correctly, create random.html file, place it where it should be read from, and try opening it via http://subdomain.yoursite.com/random.html. Then you can try some stuff like:
RewriteRule ^random.html - [F]
...and if that blocks access to file, try prepending
RewriteCond ${SERVER_NAME} ^subdomain\.yoursite\.com$ [nc]
to previous rule, to block access to that file, to make sure that rewrite engine is actually hitting your rules. That would target only desired subdomain (www.yoursite.com/random.html should work, but access via subdomain shouldn't).
Then if those rules work, it's just a matter of adding more stuff and see when it stops working.
RewriteRules are a bitch.
The following should work:
.htaccess:
RewriteCond ${SERVER_NAME} !^(www|ftp|mail)\.example\.com$
RewriteCond ${SERVER_NAME} !^([^.]+)\.example\.com$
RewriteRule .* redirect.php?to=%1
redirect.php
<?php
$desired_server_name = 'http://example.com';
$subdir = 's_' . $_GET['to'];
$url = $desired_server_name . '/' . $to . $_SERVER['REQUEST_URI'];
// Permanent redirects
header('HTTP/1.1 301 Moved Permanently');
// Or simple redirects:
header('HTTP/1.1 302 Found');
header('Location: '.$url);
?>
Works on my server (debian 4/apache 2).
Bonus: do not EVER use HTTP_HOST! See the following request:
HTTP/1.1 GET /foo/bar.php
Host: www.host.tld"><script>alert(/Hello/)</script
Connection: close
If you use $_SERVER['HTTP_HOST'] in your .php scripts to construct links or .htaccess rules for that matter and "www.host.tld" is the virtual-host or the only host configured for Apache, the XSS in the HTTP request header will be passed down unescaped.
We have a similar thing working on our Virtual Machines, where we redirect anything.usertld to a folder for that domain, that was in httpd.conf, tried in in the .htaccess and like yours it didn't work.
Tweaking it, this works for me (my VM occupies a tld called benb, but changing it to your domain should be fine):
RewriteCond %{HTTP_HOST} !^www\.benb
RewriteCond %{HTTP_HOST} ^(.*)\.benb
RewriteCond %{REQUEST_URI} !^/{0,1}s_
RewriteRule ^(.*)$ s_%1/$1 [L]
Also this captures all the text before the domain.. you should be able to change:
RewriteCond %{HTTP_HOST} ^(.*)\.benb
to
RewriteCond %{HTTP_HOST} ^([^.]+)\.benb
to handle just 1 level of subdomain. Also your other part about (www|ftp|mail) would work fine too.