nginx - Replace page content on homepage - string

I've been having a bit of difficulty setting up nginx on my ubuntu server. Right now its configured to proxy another website. I want to add some content to the main page right after <body>. I used:
subs_filter '<body>' '<body>' My content;
This works great, the only problem is that it's at every page, How can I use an if statement or something to make it only appear on the homepage. I tried going with
if ($uri ~ 'index.php') then do the above filter but that gives an error saying nginx: [emerg] "subs_filter" directive is not allowed here.
I looked it up but had a lot of trouble finding what I need :(.

According to nginx documentation subs_filter may in used in the http, server and location contexts.
Thus, in order to have the filter activated only for the index.php page,
location = /index.php {
subs_filter '<body>' '<body>' My content;
# other things you would do for index.php
}

Related

Why won't MAMP let me chage the root folder to something other than default. I get secure connect failed

I have been trying for around a year off and on to use my mac as a development webserver. I keep running into roughly the same problem no matter what platform I use. I've currently installed MAMP however when I set my root directory to my web page I keep getting security issues from whatever browser I'm using. The default root of MAMP works, no problem but when I change it to what I want I get: Secure Connection Failed. Using the MAMP default if there's an http prefix it's just http but when I set my own root the prefix is https://. The "Problem Loading Page" page doesn't give me an option to add a security exception. This is beyond my pitiful brain level to figure out, so I'm hoping much smarter people than me can help me understand what's going on. I guess I could just move my web site to the MAMP default folder but that seems like cheating...
I figured it out, finally. I kept changing the Document Root to directories leading up to httpdocs. Each worked when loading the page until I arrived at the httpdocs directory, at which time I got the security exception and problem loading page. I removed this redirect from .htaccess in htttpdocs -
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This fixed the security exception.

Getting 404 page on refresh on AngularJS site?

I am getting a served the 404 page whenever I :
1.) refresh any page on my Angularjs site (besides the root page)
or
2.) click Back to go from a 404 page to the root page (the root page will be a 404)
Here is my .config() code
'use strict';
angular.module('mmApp', ['ngResponsiveImages'])
.config(function ($locationProvider, $routeProvider) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix = '!';
$routeProvider
.when('/', {
templateUrl: '/views/design.html',
controller: 'MainCtrl'
})
.when('/about', {
templateUrl: '/views/about.html',
controller: 'MainCtrl'
})
.when('/contact', {
templateUrl: '/views/contact.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
});
I have come across this similar question but do not quite understand the answers or how to implement them.
AngularJS - Why when changing url address $routeProvider doesn't seem to work and I get a 404 error
The accepted answer says to setup Apache to reconfigure all paths to the root. My site is on Github Pages. I have a .htaccess file in my gh-pages repo but I have no idea how to configure it to send all paths to the root.
The answer says another thing to consider is using the <base> element like <base href="/" /> but where would I place that? In my index.html? If so at the top or bottom of that file? Also, would I just leave the href value to /?
I came across a solution here: https://coderwall.com/p/kfomwa
Here's the short post:
angularjs provides html5Mode, which makes your app use pushstate-based URL instead of hashtags. However this requires server side support, since the generated urls need to be rendered properly as well.
This actually works fine with github pages' custom 404 pages, though it's only available for custom domain enabled pages.
First, copy everything inside index.html to 404.html. Then, add this to your app:
angular.module('app', []).config(function($locationProvider) {
$locationProvider.html5Mode(true);
});
Note that if you are on angular 1.1.5, make sure you set for html5Mode to work properly.
Following these suggestions I was able to get my site working properly. Now when I hit Reload, the page loads properly.
The better option would be to redirect any url to the index.html:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.html
This will NOT cause a 404 response on any page.
Github pages only serves static files. It won't read any configuration settings you have for Apache in .htaccess.
If you want to run an AngularJS app out of Github pages, you cannot use html5Mode.
I'm running my angularjs application using Nginx on Vagrant. And facing the similar kind of issue.
On reloading the browser with any url, it gives me a 404 page.
I have enabled the html5mode in main js file. When you have html5Mode enabled, the # character will no longer be used in your urls. The # symbol is useful because it requires no server side configuration. Without #, the url looks much nicer, but it also requires server side changes.
Here are some changes to do:
Open nginx configuration file (nginx.conf) in /etc/nginx/sites-enabled folder (path will vary based on your nginx installation directory).
Find try_files $uri $uri/ =404; and replace it with try_files $uri $uri/ /index.html;
Save the nginx configuration file
Restart the nginx sudo service nginx restart
And I have tried to reload the page after doing above changes. It works as expected.

Wordpress MU Cloudfront. Trailing slash redirect goes to wrong domain

I'm having some trouble with redirects within wordpress redirection causing the domain to change.
Example:
Site - noncdn.somedomain.com
CDN URL - www.domain.com
When I open links w/o a trailing slash there is a 301 redirect:
Going here: www.domain.com/page
Takes you here: noncdn.somedomain.com/page/
Since Cloudfront is hitting the server using Origin Domain, the server doesn't even know that requests are coming in from a different domain.
How do I force this 301 to use FQDN w/ correct CDN domain instead of doing a relative redirect?
I've already added this so that links on the site and images all load from Cloudfront domain, but it seems to have no effect on the redirect behavior:
add_filter('home_url','home_url_cdn',10,2);
function home_url_cdn( $path = '', $scheme = null ) {
return get_home_url_cdn( null, $path, $scheme );
}
function get_home_url_cdn( $blog_id = null, $path = '', $scheme = null ) {
$cdn_url = get_option('home');
if(get_option('bapi_site_cdn_domain')){
$cdn_url = get_option('bapi_site_cdn_domain');
}
$home_url = str_replace(get_option('home'),$cdn_url,$path);
//echo $home_url;
return $home_url;
}
Any Help is much appreciated!
Thanks!
I was tracking down a very similar issue for a while with a Cloudfront distribution of a standard static website running on Nginx. The symptoms were the same, links with a trailing slash (e.g. www.acme.com/products/) worked correctly, but omitting the trailing slash caused the user to be redirected to the origin.
The issue is that the webserver software itself is not properly attempting to resolve URIs and is instead responding with a redirect to a URL it can serve. You can test this by using curl against your site:
$ curl http://myhost.com/noslashurl
HTTP/1.1 301 Moved Permanently [...]
CloudFront is returning exactly what your server returns, in this case a 301 redirect to your origin URL. Instead of following the redirect and caching that, CloudFront caches the redirect itself. The only way to correct this is to ensure that your origin properly handles the requests and does not respond with a 301.
In my particular case, that meant updating the try_files directive for my location in the nginx configuration. As I mentioned this is a static site, and so my try_files became:
location / {
[...]
try_files $uri $uri/index.shtml /index.shtml =404;
}
You want to be sure that the try_files has an endgame, to avoid redirection cycling which will cause your server to return 500 Server Errors when a non-existent URL is requested. In this case, /index.shtml is the last-ditch attempt and failing that, it will return a 404.
I know this doesn't precisely answer your question, but yours was one of a very few I found when searching for "cloudfront without trailing slash redirects to origin", and you've not had an answer for a year, so I figured it was worth sending a response.
I had the same problem.
I fixed the issue changing some wordpress parameters.
In the elasticbeanstalk I set the parameter CUSTOM_URL for my custom domain and in the file /var/www/html/wp-includes/load.php
I set the parameters HTTP_HOST and SERVER_NAME to same value of CUSTOM_URL, and it resolved the redirect to elasticbeanstalk url.
$_SERVER['HTTP_HOST'] = $_SERVER['CUSTOM_URL'];
$_SERVER['SERVER_NAME'] = $_SERVER['CUSTOM_URL'];

I would like to 410 an entire directory - I deleted my blog

I had a folder called blog on my site.
I deleted it all permanently.
I would like to 410 it.
How do i 410 an entire folder?
e.g. my site looked like this
example.com/blog/mycoolpost1/
example.com/blog/mycoolpost2/
example.com/blog/mycoolpost3/
example.com/blog/mycoolpost4/
now posts 1,2,3,4, are dead.
so how do i specify that everything after blog, is permanently deleted. (as well as the folder 'blog' itself)
I need a htaccess line something like this...?
redirect 410 /blog/?(.*)
The Redirect directive is the proper way to do this. You should put the following in your virtual host configuration:
Redirect 410 /blog
If you don't have access to the virtual host configuration, you can put it in the .htaccess file in your document root, or I believe you can put the following in the .htaccess file in the blog subdirectory:
Redirect 410 /
(I might be off about that, I'm not sure how exactly Redirect interacts with path resolution in .htaccess files)
I don't think Redirect is the right tool for this, as it only matches the path specified. Just use:
RewriteEngine On
RewriteBase /
RewriteRule ^blog/ - [G]
The following .htaccess would be useful when, for example, you move from a hosting to another and you reorder or delete parts of your web.
As Apache allows human syntax codes I have used permanent instead of 301 code, and gone instead of 410. You can check http protocol codes here Status Code Definitions
I placed the file on my root mynewblogaddress.com folder:
.htaccess
Redirect permanent /wordpress http://www.mynewblogaddress.com/blog/
Redirect gone /gallery2
Redirect permanent /directory2 http://directory2.mynewblogaddress.com
You can set a rewrite rule in .htaccess to redirect all URLs containing the dead folder "blog" to a custom "Does not exist" error page or whatever. If you want the actual code then I would recommend reading guide-url-rewriting to help you figure this out.
For those who are using IIS (7 or above) and stumble across this post as i did, this is how I wound up doing it using the global.asax:
void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app.Request.Url.PathAndQuery.IndexOf("/mydirectory") > -1)
{
Response.StatusCode = 410; Response.End();
}
}
I happened to be looking for all pages in a directory, but I could have done something similar like targeting all html pages (assuming 410’ing ALL html pages were to be “gone”)

Index_file and kohana 3.1.2

Adding 'index_file' => FALSE to the Kohana::init successfully removes the index.php part of URLs, but when a page is redirected to website.com/controller/action (note: there is no index.php before controller) I get a 404 Not Found error.
How is lighttpd supposed to know to call the index.php file at the base instead of looking for a controller/action folder (which it seems to be doing and thus getting a 404)?
You need to use URL rewriting.
I would port Kohana's .htaccess to lighttpd.

Resources