Cloudflare page rules for removing multiple trailing slash - cloudflare

I want to set up the below things in Cloudflare.
https://example.com// OR https://example.com// OR https://example.com/// OR onwards
are redirecting to https://example.com/
How can I set up this using the Cloudflare page rule?

It will be quicker to set that in your webserver(Nginx) virtual host than doing it on Cloudflare.
Add below in your Nginx config:
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) https://$host/$1 permanent;
}

Related

Convert Apache RewriteCond to Nginx

I want to convert following Apache config to Nginx compatible config.
What it currently does is redirect all traffic which DOESN'T contain ^/nexus/content* in uri to HTTPS. That means even if I access http:// example.com/nexus only, it should go to HTTPS.
Bottom line is I want keep http:// example.com/nexus/content* in HTTP but http:// example.com/nexus should redirect to HTTPS. Hope the question is clear :)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/nexus/content*
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I tried following Nginx configs but didn't work so far. Appreciate any help.
if ($request_uri != ^/nexus/content*) {
rewrite (.*) https://example.com$request_uri;
}
And
location ~ ^/nexus/content* {
}
location / {
rewrite ^(.*)$ https://$http_host$request_uri redirect;
}
I'm not sure if this will work, but here goes:
server {
listen 80;
server_name example.com;
if ($request_uri !~ "^/nexus/content*") {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
add_header Strict-Transport-Security "max-age=31536000"; # MiTM Mitigation
// your normal rules here
}
Similar to yours, but it does a redirect and not a rewrite.

nginx rewrite / proxy_pass problems

I did some extensive searching and there are quite a few posts on nginx proxy_pass questions. I've tried modifying my problem to some of those questions with out any progress so here goes.
I'm in the midst of rewriting a php based website into Rails. The original site itself is simple 2 pages with forms. It uses a mod_rewrite / mod_proxy solution in Apache to mask the url of where the site continues after form post.
the php site has 3 directories that have nothing but 3 .htaccess files in them for simplicity sake lets call the directories a, b, c. each .htaccess file with the following
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.domainbeingpostedto.com/actual_letter_directory/$1 [P,L]
</IfModule>
I'm not an apache expert but I'm pretty sure the [P,L] is same as proxy_pass and last in nginx?
I'm trying to rewrite this solution for the php site that was converted into a rails cms using passenger and nginx instead.
The solution I have so far is not working because the rails app just returns a 404 with the page not found, so I know proxy_pass isn't forwarding the post request to the other server.
What I have for my nginx.conf file is:
server {
listen 80;
server_name newtestappdomain.com;
location /a/ {
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
#rewrite ^/a/(.*)$ http://www.domaintoacceptdatapostandbemaskedcom/a/ last;
}
location /b/ {
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
#rewrite ^/b/(.*)$ http://www.domaintoacceptdatapostandbemasked.com/b/ last;
}
location /c/ {
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
#rewrite ^/c/(.*)$ http://www.domaintoacceptdatapostandbemasked.com/c/ last;
}
root /home/deploy/testapp/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
If I uncomment out the rewrite rule, it just bounces to the other site which i'm trying to mask. I did a header trace to verify as well. Didn't see anything post to the other domain. I'm kind of stumped as I'm really new to nginx and not sure what to do. Apache doesn't work well with rails and mod_rewrite / mod_proxy. Any insight would be great.
proxy_pass http://www.domaintoacceptdatapostandbemasked.com/;
This rule would be proxy requests to "/" location (leading slash in proxy_pass uri) (a/, b/ and c/ would be lost).
Just use uri without leading slash and it should work perfect
proxy_pass http://www.domaintoacceptdatapostandbemasked.com;
If you need to change uri, you can use rewrite before proxy_pass.
for example:
location /a/ {
rewrite /(a/.*)$ /actual_letter_directory/$1 break; # as from you .htaccess example
}

Redirecting wildcard subdomains to a different top-level domain with nginx

We have a bunch of wildcard subdomains (_foo.example.com, bar.example.com, etc) that, when accessed via HTTPS should redirect to the equivalent subdomain on our secure domain.
Some examples:
https://foo.example.com => https://foo.secure.com
https://foo.example.com/some/path => https://bar.secure.com/some/path
https://bar.example.com => https://bar.secure.com
I think this can be accomplished with nginx rewrites but I'm not sure about the syntax. Here's what I'm attempting:
server {
listen 443;
server_name *.example.com;
rewrite ^(.*) https://*.secure.com$1 permanent;
}
This obviously won't work because I'm not capturing the incoming subdomain and using it in the rewrite.
Try something like this (untested):
server {
listen 80;
listen 443 default ssl;
server_name "~^(?<name>\w\d+)\.example\.com$";
rewrite ^(.*) https://$name.secure.com$1 permanent;
}
Found this on http://forum.slicehost.com/comments.php?DiscussionID=730
# redirects arbitrary subdomain (some.random.sub.example.com) to (some.random.sub.example.org)
if ($host ~* "^([^.]+(\.[^.]+)*)\.example.com$"){
set $subd $1;
rewrite ^(.*)$ http://$subd.example.org$1 permanent;
break;
}
# Simply redirects example.com to example.org
if ($host ~* "^example.com$"){
rewrite ^(.*)$ http://example.org$1 permanent;
break;
}

how to turn URL to the new domain?

old domain link:
http://www.old.com/aa/bb/cc.html
i want when this old link to be visiting,turn to:
http://www.new.com/aa/bb/cc.html
how to do this?
BTW: nginx or apache
Create a .htaccess file similar to the following and place it to the old domain root directory.
RewriteEngine On
RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,QSA]
For nginx I go with this on account that it also helps with alias domains.
if ($host != 'www.new.com' ) {
rewrite ^/(.*)$ http://www.new.com/$1 permanent;
}

Simple rewrite rule for a nginx + apache2 with mod_wsgi

I'm stuck with this, my skills in the web servers area are poor...
I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, i.e. stripping the www part from each URL request before serving. This is the layout of the different conf files:
=== /etc/nginx/sites-enabled/example.com ===:
http://dpaste.com/82638/
=== /etc/apache2/sites-enabled/example.com ===:
http://dpaste.com/hold/82645/
=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:
http://dpaste.com/82643/
In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That worked perfectly.
But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever, everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:
http://example.com/example.wsgi/
Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:
rewrite ^/(.*) http://example.com/$1 permanent;
but now I am getting what firefox calls a "circular loop"...
So who can I get this (I guess trivial) thing up?
Thanks in advance,
Hector
The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com
server {
listen 80;
server_name www.example.com;
rewrute ^/(.*) http://example.com/$1 permanent;
}
All right I found the solution to avoid the circular loop... by creating TWO server sections in my nginx config file, one for www.example.com -- which has the rewrite rule suggested by rzab -- and the other for example.com, which contains all the rest of directives.