Apache .htaccess redirect from https to non https for single URL - apache

Ok, researched this one to death here and on Google. Trying to get an https URL to redirect to a non HTTPS URL.
I have tried all kinds of combos but, nothing is working for this URL:
https://www.urotoday.com/403-perspectives-from-the-editor-in-chief-june-2014
All I want to do is redirect to http://www.urotoday.com/403-perspectives-from-the-editor-in-chief-june-2014
This didn't; work for me:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/403\-perspectives\-from\-the\-editor\-in\-chief\-june\-2014$
RewriteRule .* "http\:\/\/www\.urotoday\.com\/403\-perspectives\-from\-the\-editor\-in\-chief\-june\-2014" [R=301,L]

Place this rule in your DOCUMENT_ROOT htaccess:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^(403-perspectives-from-the-editor-in-chief-june-2014)/?$ http://%{HTTP_HOST}/$1 [R=301,L,NC]
Let me know if this works. :)

Related

Not able to rewrite and redirect API URI in Apache htaccess

I am learning Apache .htaccess configuration. I would like to apply https to a URL if domain is mydomain.com only
if URI stars from API and followed by digits
Example--> "http://exampledomain.com/api/123456" to be redirected to https://exampledomain.com/index.php?val=123456 in backend server.
I have tried the below but it isn't working. I have not been able to get the API link also to properly rewrite. Any help would be highly appreciated.
RewriteEngine On
RewriteRule ^api/([0-9]+) index.php?val=$1 [NC,L]
RewriteCond %{HTTP_HOST} ~exampledomain.com [NC]
RewriteRule (.*) https://%{HTTP_HOST}$1 [R,NC]
Could you please try following, written and tested with shown samples. You were close try to put your https redirection rules first in starting of your Rule file and then proceed with further.
RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(exampledomain\.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^api/(\d+)/?$ index.php?val=$1 [NC,L]

How to prevent homepage 301 chains?

I'm trying to make all versions of homepage URLs 301 to same place, without a 301.
It's difficult to show the problem because I don't have enough rep points to post image or show the http response codes in a chart (too many links!)
But, using the code below https://www.example.com/ goes to http://www.example.com/ first, before it 301s to the homepage URL http://example.com
I'm on apache, and I've been using .htaccess to try to resolve this.
I've tried also tried the following, but it only works for file paths.
Redirect 301 /oldfile.htm /newfile.htm
# Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example/$1 [L,R=301]
I've tried also tried the following, but it only works for file paths.
Redirect 301 /oldfile.htm /newfile.htm
Is there a way I can make https://www.example.com/ go to http://example.com without the 301 chain?
Thanks,
Mike.
You can use a single rule to redirect your https URLs to http and non-www version. This will redirect all of your https urls to http without creating multiple redirect chain.
Replace your htaccess rules with this :
# Redirect HTTPS to HTTP and non-www
RewriteCond %{HTTP:X-Forwarded-Proto} =https [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301]
Make sure to clear your browser cache or use a different web browser to test this rule.

htaccess redirect for https, www and com to co.uk

I've been trying to solve this issue for hours now to no avail. To get to the point, I have identical domains with different TLDs, .com and .co.uk. Here's my htaccess file at the moment:
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# com to co.uk redirect
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
I've been trying to test the config here: http://htaccess.mwl.be/ but I'm getting some strange results, like the .com domain being appended to the .co.uk one.
It also seems like some rules work while others don't. The aim is all variations of example.com redirected to https://www.example.co.uk eg:
www.example.com
http://www.example.com
https://example.com
Can anyone see where I'm going wrong with this? I'm not very experienced with htaccess redirects so any help is appreciated.
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [NE,L,R]
The rule above will redirect :
http://www.example.com
to
https://www.example.co.uk

Redirect certain pages from http to https; redirect rest of site to www

My requirements are as follows:
Certain pages of my site need to use https
The rest can use http
I also need to redirect everything to use a www. prefix (e.g., if someone visits domain.com it redirects to www.domain.com)
I've tried a number of solutions listed here on Stack Overflow, but none seems to work.
Here's the relevant portion of my htaccess file:
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(login2\-test\.php.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
#redirect all pages to www
RewriteCond %{HTTPS} !=off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Redirecting all pages to www works fine. However, the redirect of login2-test?Switch=ftr (which is in the reg/ subdirectory) to https results in no match, so the page is displayed using http.
I've rewritten that line as follows:
RewriteRule ^(reg\/)(login2\-test\.php.*)$ https:www.domain.com/$1$2 [L,R]
This at least matched / redirected to https, however the browser could not resolve it.
I've swapped the order of the rules (e.g., www redirect first); that didn't help.
I would appreciate any help -- I've been struggling with this for a while now.
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]

Apache redirect all to www https

I am currently using the following in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
..however I also want all requests to be redirected to https://www.example.com. As it stands any requests for http://example.com or https://example.com go to https://example.com. I have tried various methods in redirecting however seem to be only able to get all requests to http www or all requests to https, I can't figure it to get both.
Also any rule needs to include the original filename requested e.g a request for http://example.com/contact.php should be redirected to https://www.example.com/contact.php.
Any help or pointers would be much appreciated. Thanks!
You can use that:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.exemple.com%{REQUEST_URI} [NE,R=301,L]