.htaccess for MODx Evo site behind Cloudflare with https - .htaccess

Looks like a bit easy question but I can't done that right, so I'd ask you for a little help:
I have some small site made on MODx Evo. It is placed behind CloudFlare (free tariff, very basic features used), mainly for their free https. Now I try to set only one address as a base one:
http://example.org
https://example.org <-- this one should be base
http://www.example.org
https://www.example.org
I do use default MODx Evo .htaccess to provide nice URLs and some basic PHP settings, which looks like this:
AddDefaultCharset utf-8
RewriteEngine On
RewriteBase /
# Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
# Exclude /assets and /manager directories and images from rewrite rules
RewriteRule ^(manager|assets|js|css|images|img)/.*$ - [L]
RewriteRule \.(jpg|jpeg|png|gif|ico)$ - [L]
# For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Here is everything but the part which does the redirect from www to non-www domain, and it is stock settings.
Now I need to do another two redirect conditions:
http -> https
www -> non-www
This is where I'm stuck. I can easily redirect non-www to www, but as I add http/https part, I'm getting 301 loop.
The site itself is on Apache (the Cloudflare adds their special Nginx version).
Please advice what should I add to the .htaccess shown above to have all 3 domain variants shown above redirected nicely to https://example.org one?

To my great surprise, this was possible to do with Page Rules on Cloudflare itself, which is the best solution as for me, since no backend (like my server behind the Cloudflare) involved.
Thanks Cloudflare!

.httacces may not work
I founded solution by edit config (https://gitlab.com/snippets/1906338)
To fix https thro cloudflare needs edit var $secured like this in manager/includes/config.inc.php - add checking cloudflare $_SERVER["HTTP_CF_VISITOR"]
$secured = (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
(isset($_SERVER["HTTP_CF_VISITOR"]) && $_SERVER["HTTP_CF_VISITOR"] == '{"scheme":"https"}');

Related

htaccess URL masking subfolder to another domain instead of redirecting

I need to redirect www.domain.com/folder to subdomain.anotherdomain.com without change the first url, www.domain.com/folder. In www.domain.com I have a WordPress site and in subdomain.anotherdomain.com a Moodle site. By that, only www.domain.com/folder must have a redirect, not the www.domain.com or www.domain.com/other_folder. It can be done? It´s possible?.
In the Worpress site I´ve this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I try with a iframe, but a Google Chrome don´t like that :/ (cookies issues)
Many thanks in advance.
You cannot somehow keep a URL pointing to one http server visible in the browsers URL bar but make it connect to some other http server. That would be a gigantic security issue.
If you can change the setup such that both host names point to the same http server, then what you ask can be achieved by applying simple some internal rewriting rules.
For separate http servers your only option is to use the proxy module the apache http server brings, if it is installed and enabled ...
ProxyPass /folder https://subdomain.anotherdomain.com
ProxyPassReverse /folder https://subdomain.anotherdomain.com
See the documentation of the proxy module for details: http://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypass
The proxy module can also be used from within the rewriting module, but this comes with an optional decrease of speed:
RewriteEngine on
RewriteRule ^/?folder/(.*)$ https://subdomain.anotherdomain.com/$1 [P,QSA,END]
Again this is documented in the rewriting module:
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_p

Redirect all subdomains and subdirectories to index page using .htaccess

I have a Detroit iOS & Android Mobile App Development website that only has one web page : index.html.
The source code of the site is here.
Instead of showing a 404 error page, I want to redirect the user to thefirstprototype.com if they try to go anywhere else or try to put anything after.
For eg:
mail.thefirstprototype.com takes the user to just thefirstprototype.com
thefirstprototype.com/mail takes the user to just thefirstprototype.com
I know it's possible to do it using a .htaccess in the root folder, but I am just not sure how. There are a lot of tutorials showing how to do it between different domains, but nothing to my specific case. How do I do it?
Thanks
Edit1: Please note that I am not using any CMS like Wordpress. I am just plain FTP to push a static HTML, CSS, JS webpage to the hosting server
Try the following:
DirectoryIndex index.html
RewriteEngine On
# Redirect non-canonical hostnames (eg. mail)
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ http://example.com/ [R=302,L]
# Redirect 404 to root
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=302,L]
However, whether this catches requests for the mail. subdomain will depend on whether that subdomain points to the same place as your main domain. (For cPanel shared hosting, that is not necessarily the case.)
Change the 302 (temporary) redirect to 301 only once you have tested that this works OK - to avoid potential caching issues associated with 301 (permanent) redirects.
As an added bonus, you could redirect any direct requests for index.html back to the root. For example, add the following between the above two rule blocks:
# Remove "index.html" if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [R=302,L]
The condition that checks against the REDIRECT_STATUS environment variable is to ensure we don't get a redirect-loop since mod_dir internally rewrites the request to index.html.

Redirect HTTPS to HTTP for one directory while ISAPI rewrite in place

I've got a site (example.com) and a blog (externalblog.com) which is hosted externally and reverse-proxied using ISAPI rewrite. So if you go to www.example.com/blog you're actually on externalblog.com, but the URL is masked.
I've got this working with a number of sites, but the issue is when the parent site uses HTTPS, whereas the blog is only HTTP. When visitors attempt to follow old links, the blog posts will show normally under the https version of the url, but the stylesheets are all broken and a "no certificate" warning is shown.
I need an htaccess rule that will do 2 things:
Forward anyone attempting to access an https version of a blog post only (ie anything within www.example.com/blog) to the http version instead.
Forward anyone attempting to visit example.com/blog (without the www) to http://www.example.com/blog instead of the https version, which it currently does.
However I've like the rules to only affect www.example.com/blog and nothing else on example.com. My current rule for ISAPI rewrite on example.com is as follows:
RewriteRule ^blog(.+)$ http://www.externalblog.com$1 [R=301,L]
This works correctly and is based on an article online called "using reverse proxying to pull a wordpress blog into your domain" – I can't add any more links here due to not having enough reputation.
Any help would be much appreciated.
EDIT: I tried the following
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/blog [NC]
RewriteRule ^blog(.+)$ http://www.externalblog.com$1 [R=301,L]
This works as far as rewriting https to http – but reverse proxy URL masking no longer kicks in.
Your explanation is rather unclear, so sorry if my answer will be unrelated. I suppose you are trying to force everything under www.example.com/blog to be HTTPS, instead of HTTP? Then try this rule:
RewriteEngine On
# HTTP to HTTPS redirect rule
RewriteCond %{HTTPS} off [NC]
RewriteCond %{HTTP:Host} ^www\.example\.com$ [NC]
RewriteRule ^(blog/.*)$ https://www.example.com/$1 [NC, R=301, L]
# No www. to www. redirect rule
RewriteCond %{HTTP:Host} ^example\.com$ [NC]
RewriteRule ^(blog/.*)$ https://www.example.com/$1 [NC, R=301, L]
# Your proxy rule goes here, I suppose it looks like:
RewriteRule ^blog(/.*)$ http://www.externalblog.com$1 [NC, P]

mod_rewrites .htaccess not working with godaddy hosting (linux)

On my site www.sqcp.com in testing on another linux server, all worked as it should. However since moving it to godaddy, the mod_rewrites haven't been working, therefore none of the other pages have been accessible. Even if I create a blank directory/folder in the what it's trying to tidy the url to it then works for that page (obviously isn't a fix).
So any help would be great here my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ / [L,R=301]
RewriteRule (.*)/{2,}$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^(.*)/(\d{4}\-\d{2}\-\d{2}\-[a-zA-Z0-9\-_]+)$ $1?s=$2 [L]
RewriteRule ^(.*/)?staff.php/([a-zA-Z0-9\-_]+) $1staff.php?s=$2 [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule (.*)/$ $1 [L]
</IfModule>
Godaddy run a perfectly good shared hosting service, addressing a large market sector -- users who want an active site (that is with some scripting) but without the cost or complexity of paying for or having the expertise to administer their own Linux VM. This isn't a GoDaddy problem. Its yours.
So first get to understand the environment that you are running under by running a phpinfo script, and make sure it creates the variables that you use. As far as I can see on your example:
Rules 1-3 are 301 redirections to enforce some request naming convention.
Rule 1 redirects /index.php to /
Rule 2 collapses trailing multiple / to a single /
Rule 3 rewrites GET requests for *.php to *.php/
Rules 4-6 map public URIs to internal ones
Rule 4 rewrites /*/yyyy-mm-dd-word to *?s=yyyy-mm-dd-word (note no QSA)
Rule 5 seems to be attempting to rewrite /*/staff.php/word to /*/staff.php?s=word but the syntax is wrong for this.
Rule 6 replaces any trailing / by .php on redirection
Rule 7 strips any trailing '/' unless the uri is a directory with an index.php (I assume that you are assuming a DirectoryIndex index.php (is this the case for GoDaddy?)
This is all hopelessly confused. Are you hiding or exposing the .php extension? Because Rule 3,5 and 6 are inconsistent. And rule 5 would seem more logical as
RewriteRule ^(.*?)/staff.php/([a-zA-Z0-9\-_]+) $1/staff.php?s=$2 [L]
Go back to the drawing board and work out what you are trying to do with your htaccess rules; what you want your public URI grammar to be; how your scripts are laid out; what redirects you want to pass back to the client browser and which you want Apache to handle as internal rewrites and what extra conditions are needed to prevent looping and misfiring. Make sure this makes sense and then debug them by building up your .htaccess file one rule at a time and using test requests to exercise each rule in turn to validate what its doing.
Trying adding the following at the start of your htaccess file. I had the same problem getting rewrites to work on GoDaddy which worked everywhere else:
Options -Multiviews

URL Rewrite seems to be functioning like a URL Redirect

This is the rewrite section of my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^komarketingassociates\.com$ [NC]
RewriteRule ^(.*)$ http://www.komarketingassociates.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
RewriteRule ^industry-news/.*[$]([0-9]+)\.htm$ http://www.komarketingassociates.com/industry-news/article.php?articleid=$1 [L]
The last rewrite rule is meant to process requests for our repository of news articles. Until maybe a week ago it was working perfectly translating the SEO friendly link titles like this one, companies-are-becoming-more-accustomed-to-leveraging-social-media$715.htm into the proper format to retrieve them from the database. Recently, however, without any changes being made to the .htaccess file (to my knowledge) the links now seem to use this redirect to article.php?articleid=715 rather than doing it server side. The links on the site are still the seo friendly urls, but now it seems as if you are being redirected to the article template page.
We are using GoDaddy to host our site. PHP version 5. I am completely stumped as to how this is happening and how to fix it. Any help is appreciated!
Thanks!
-Bill
Proxy flag. (P)
Warning, will mess with IPs. Every single IP will look like it's your servers. However, mod_rewrite sets the X-FORWARDED-FOR when you use P, so you can extract (I'd recommend validating the IP as being real as well before trying to use it in any SQL) and use the IP address still.

Resources