I'm a newbie to coding and I'm using following codes in my .htaccess.
My issue is my site is not redirecting from http://example.com to https://example.com with this code. Did research but couldn't find something that goes with my code.
I do not know if this is the correct order, or the most efficient way to achieve this. Any input would be greatly appreciated!
Remove WWW, from both HTTP and HTTPS an force HTTPS
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ https://%3%{REQUEST_URI} [R=301,L]
Hide .php, and redirect .php files to new non-extension URLS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
Give this a go and let us know if it works out for you. looking at your .htaccess for your SSL looks like it has way more in it then there needs to be. This is what I use and seems to work fine
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This will redirect all traffic to an https://
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 have a website that has custom subdomains for users, e.g. creator.snaprate.me.
This works great, but if I manually type in creator.snaprate.me/login the login page (snaprate.me/login) appears.
I'd like to have a redirect to snaprate.me/anypage for any subdomain, but don't know how.
I have the following .htaccess, and this has been working great except for this issue:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^((?!www\.)[^.]+).snaprate\.me$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+).snaprate\.me$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
What do I need to add in order to redirect traffic from subdomain.snaprate.me/anypage to snaprate.me/anypage? I'd really like an answer to this; I have been searching for days and still I have no idea what to do -- htaccess is my Achille's heel!
I'm trying to redirect foo.bar.com to bar.com/foo.php, but I have no idea where to start and everything else has failed me so far. I've tried many things here on StackOverflow, but none of them seem to be working at all (just various Apache errors).
I already have a wildcard subdomain set up from cPanel, but I'm not really sure where to go from here. Is .htaccess even the best option for this?
EDIT: I also want to rewrite the URL from bar.com/foo.php to foo.bar.com.
You can use this generic rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ http://%2/%1.php [L,R=302]
EDIT: To hide the rewritten URL (internal rewrite) use this:
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ %1.php [L]
from
cdn1.example.com
cdn2.example.com
cdn3.example.com
to
example.com
Please let me know how to redirect those?....
You could have provided more details and also what you have tried, anyways here you go:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(cdn1|cdn2|cdn3)\.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
As a bonus, any eventual path and/or querystring is preserved, I guess you would need that.
EDIT : As you requested, here is a cdnx version:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn[0-9]+\.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
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]