I've tried implementing a number of suggested solutions to this problem but can't get anything to work.
I am using 2 .htaccess files, one in my root directory which reads as follows:
RewriteEngine on
RewriteBase /
RewriteRule ^(webservice|api) - [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [R=301,L]
This redirects all traffic to the main website through https which I need.
However in a subdirectory I want to disable the reroute and force all requests that go to this directory through http. The .htaccess file in /webservice/rest/ reads:
RewriteEngine On
RewriteBase /webservice/rest/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.domain.com/webservice/rest/%{REQUEST_URI} [R=301,L]
RewriteRule ^testingPost/?$ testingPost.php [NC]
RewriteRule ^([a-zA-Z0-9\_\-]+)/?(\?([a-zA-Z0-9\_\-\=\?\&]))?$ otherfiledirect.php?method=$1¶ms=$2 [NC]
There are a few redirects taking place, but the 3rd and 4th line in the 2nd htaccess don't seem to be doing anything - can anyone see what I'm doing wrong and how to keep https on the main site but force http on any url below /webservice/rest/ ??
Cheers
The problem was with the ssl config file in the server. Allow Override needed to be set to All under the secure virtual host at port 443. After changing that, the following code in the root htaccess file allowed me to force https across the whole website except a specific folder:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/webservice [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
So any request to http would go to https. However I still needed to explicitly change requests to https on my specified folder to redirect to http, so added the following code after the above:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} /webservice [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Bullet proof.
Try this in /webservice/rest/.htaccess and make sure to test in a new browser:
RewriteEngine On
RewriteBase /webservice/rest/
RewriteCond %{HTTPS} on
RewriteRule ^ http://www.domain.com/%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^testingPost/?$ testingPost.php [L,NC]
RewriteRule ^([\w-]+)/?(\?([\w-=?&]))?$ otherfiledirect.php?method=$1¶ms=$2 [L,NC]
Related
I am trying to redirect to
https://example.com from https://www.example.com, http://example.com, http://www.example.com
------------------ and ------------------
https://example.com/sub_page/pages from
https://www.example.com/sub_page/pages, http://example.com/sub_page/pages,
http://www.example.com/sub_page/pages
on .htaccess but my sub_pages are not redirecting properly. probably because of conflicting codes below. I read many articles on Internet but do not fount useful.
my .htaccess file
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think you got my whole point. In short, I want to redirect all requests to non=www and https. Please help me with this,
This probably is what you are looking for:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
That should work in the actual http server configuration or, if you have no access to that, in a distributed configuration file (".htaccess) located in the http hosts's document root.
i use mod_rewrite to allow "friendly URLS" to replace search parameters, which works fine.
However i need to now redirect all traffic to https:// but allow only some pages to remain on http://
(i need to do this so my referrals are collected by the sites i send traffic to)
Over the years i have added to the .htaccess file, most works OK but i dont fully understand it, so it may be getting messy too :-(
i have the below (i have cut out anything i dont think is relevant)
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([A-Za-z0-9'/-]+)-([0-9]+)$ profile.php?id=$2 [NC,L,QSA]
# NOTE: this allows for profile.php?id=123 to be replaced with /profile/name-123
# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
The above works OK, but redirect ALL pages to https
I have tried adding /changing the https redirect part to the below to allow urlout.php not to be redirected, but this just loops and i get a browser warning that the page has too many redirects :-(
# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteCond ${REQUEST_URI} !^/urlout\.php
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have tried a few other things too, but they either dont redirect and everything goes to https, or i get a loop error or internal 500 error...
Basically i want all pages to go to https, but not urlout.php
Any advice on any of the code above would me much appreciated!
Answering my own questions:
i managed to do this, was a silly mistake of me not adding [NC] after the first condition...
However, for others, the below code will redirect ALL to https:// from http:// with the exception of 1 page (urlout.php in my case)
# redirects any http:// traffic to https:// with exception of urlout.php
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !^/urlout\.php [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I needed to do this, so that sites i send traffic to can see my domain as the "HTTP referrer".
I however found a simpler way to pass the referrer from https to http (although browser specific!) of adding to the HTML head:
<meta name="referrer" content="origin">
My ref for this was (among others): moz.com's Meta Referrer Tag Blog Post
If you want to redirect some spacific pages to https, forexample, To redirect :
http://www.example.com/page.html
to
https://www.example.com/page.html
And
http://www.example.com/page2.html
to
- https://www.example.com/page2.html
You may try the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
If you want to redirect the whole site from http to https, you can use the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]
RewriteCond %{HTTPS} off is important to avoid redirect loop as it skips the rule for https requests.
My problem is that when I try to access the some file, for example
http://domain.com/myFile.php?Key1=Value1&Key2=Value2
Sometimes it works, and sometimes it redirects me to:
https://domain.commyfile.php/?Key1=Value1&Key2=Value2
this problem started after I added TLS to the site (redirecting http to https works fine, But this problem showed up).
my .htaccess file:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain$1 [R,L]
RewriteEngine On
RewriteCond %{HTTPS} on
can anyone tell me why does it happen?
You need to change the RewriteRule because the base is /. Do the following:
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTPS} on
I have application based on PHP and Apache. If requested link is not directory or file, all requests go to index.php file. I wanna redirect all requests from http to https and without www.
Valid link for me:
https://someaddress.org
Started from following links invalid for me(sorry my reputation is very small and i cant post more 2 links):
http://
http://www.
www.
My htaccess looks like that
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [L]
How i can do redirects to https and without www?
Have another rule for http->http and www removal:
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://someaddress.org%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
You can cover the http to https part with the SSLRequireSSL directive.
<Directory />
SSLRequireSSL
</Directory>
If the problem is the htaccess file and not the rewriting rules you can stop using the .htaccess file and move the rewriting rules to the apache configuration files.
If you just dont want or can use rewriting rules, and as the vast majority of php applications needs a configuration file included in every single script (configuration vars or user control as examples) you can use this file to get the requested resource and strip the www. part if the domain name starts with them.
I can provide a php code example if the include file may be the solution you need.
First redirect all request with WWW to https without WWW
# For http://www.yourdomain.com and https://www.yourdomain.com
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]
Now redirect all http request without WWW to https without WWWW
# For http://yourdomain.com
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]
Add above code one by one in .htacess file in sequance to redirect your all request to https without www
I used "R=301" for permanent redirection
I've a site to work and trying to make redirect by htaccess for few days. I've searched the net and found good works, especially in this site, but altough I've tried almost every possibilities, I've could not achieve what I want to do.
My need is redirect all site to non-www http, including https, except for only one file. Let's say redirect
http ://www.example.com/.../....php?a=...
https ://www.example.com/.../....php?a=...
https ://example.com/.../....php?a=...
to
http ://example.com/.../....php?a=...
However, only one specific file
http ://www.example.com/.../theSpecificFile.php?a=...
https ://www.example.com/.../theSpecificFile.php?a=...
http ://example.com/.../theSpecificFile.php?a=...
should be redirected to
https ://example.com/.../theSpecificFile.php?a=...
To do these, I've wrote many htaccess files but in each case, I couldn't achieve my needs. e.g. At the last htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 http://example.com/404.php
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(theSpecificFile\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#redirect www.example.com to example.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
By this htaccess file,
when I try to https ://www I get Unsecure or Untrusted connection (I'm translating from another language, hope it might be true translation) with ssl_error_bad_cert_domainand when I try to access to the theSpecificFile.php I get error defining "infine loop" (again I hope this might be a true translation).
This is really frustrating for me, so, any help would be highly appreciated.
Thanks in advance
Look, here you redirect theSpecificFile.php to its https version, but then the second redirect rule triggers and redirects the browser back to http, and from there the first rule redirects back to https... That's why you're getting the infinite loop.
And ssl_error_bad_cert_domain means the certificate isn't for this domain, so you might need to check if you're using the right one.
You can use that:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{REQUEST_URI} !/theSpecificFile\.php [NC]
RewriteRule ^ http://example.com/%{REQUEST_URI} [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /theSpecificFile\.php [NC]
RewriteRule ^ https://example.com/%{REQUEST_URI} [L,R=302]
Change [R=302,L] to [R=301,L] when everything works well
Please try:
RewriteCond %{HTTPS} on
RewriteConf %{REQUEST_URI} !=/theSpecificFile.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]