Redirect all to HTTPS - .htaccess

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.

Related

htaccess redirect puts old path in parameter

I want to forward one specific URL to another one. Tried this:
Redirect 301 "/stellenangebote-berlin" "http://mydomain.de/stellenangebote-in/berlin"
The forward works, but the old URL is appended as parameter like so:
www.newURL/deeplink.php?path=stellenangebote-berlin
This breaks my further routing. How can I prevent the script from adding the old path?
Thanks!
Did you try like below because I'm using the below way in my server and it's working fine.
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.de/stellenangebote-in/berlin [R=301,L]
Or you can try it in one more way like below.This one redirects everything after the domain name on the url to the exact same copy on the new domain url:
RewriteEngine on
RewriteRule ^(.*)$ http://mydomain.de/stellenangebote-in/berlin$1 [R=301,L]

redirect 301 not working properly in .htaccess

htaccess 301 redirect. I have this old site which is for example http://test.org/conference I want to redirect it to conference.test.org
What happened is the 301 redirect in my .htaccess file is quite buggy.
Here is my 301 redirect htaccess code below:
RewriteCond %{REQUEST_URI} ^/http://test.org/conference(/)?
RewriteRule ^(.*)$ http://conference.test.org/? [R=301,L]
When I test this one it runs and redirects correctly. But when I test it over and over again. It seems not to redirect anymore.
Can someone help me have a htaccess 301 redirect code?
Any help is much appreciated.TIA
I assume this is what you are looking for:
RewriteEngine on
RewriteRule ^conference/?$ http://conference.test.org/ [R=301,L,QSA]
Please note that %{REQUEST_URI} only contains the path of the URI, so not the protocol and the hostname. Reason is that the evaluation is performed inside a http host. This is explicitly pointed out in the documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Depending on your http hosts setup you might also have to add a condition to prevent an endless redirection loop:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^conference\.test\.org$ [NC]
RewriteRule ^conference/?$ http://conference.test.org/ [R=301,L,QSA]
But usually that is not required, since the rule should be defined inside the test.org http host...

htaccess rewrite rule for subdomain results to redirect loop

Below is my htaccess config
RewriteEngine On
# if the domain starts with "apply."
# i.e. apply.domain.com
# redirect to application URI
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteRule !^formwizard /formwizard/apply [R=301,L,NC]
I have tried it, and when I go to http://apply.domain.com/ it successfully redirects to http://apply.domain.com/formwizard/apply.
My problem is, once it redirects to that URI, it goes to a redirect loop.
Can anyone please help hit me what is wrong with my config?
Background:
I have 3 subdomains: www.domain.com, apply.domain.com, and admin.domain.com. It all references to the same source codes and just managed by my htaccess.
Once the person goes to apply.domain.com, it should be redirected to the application page which is /formwizard/apply.
PS. I don't want to depend too much on the backend codes because I believe this is a server config problem.
Thanks in advance!
If there are more rules then this rule might be getting impacted by them. Modify this rule to this:
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteCond %{THE_REQUEST} !/formwizard/ [NC]
RewriteRule ^ /formwizard/apply [R=301,L,NC]
Also make sure this is very first rule below RewriteEngine On line.

.htaccess subdomains redirect is not working

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.

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Resources