I want to have /blog directory on my application to have an instance of WordPress installed there. Currently /blog does takes me there, but I do want to know and have control, instead of directory how to read from routes.php.
There can be many use cases for it as blog directory is under maintenance or its down, in that case a simple change so it starts reading /blog from routes.php.
I am not much expert of '.htaccess' file but can we do from it?
if your CI application use the default routes configuration, then /blog will make CI try to load blog controller and index action, if it can't find them. you'll get 404 page not found error.
try to config the /blog in your web server configuration. add /blog block then apply wordpress server config there. make sure that request will find /blog location block before the / location block.
in nginx:
location /blog {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
# then your ci / location
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
Related
We have a custom PHP application that we wrote and runs on Apache with .htaccess files to handle the url rewrites. We are trying to convert it to work under NGINX with FPM under Plesk Onyx.
The application generates links like:
https://somedomain.com/mypage (same as index/mypage)
https://somedomain.com/index/sitemap
https://somedomain.com/blog/some-article-name
These URL's map to index.php files that take the request_uri and use it to render the page responses.
The structure of the application is nested as follows:
docroot (/)
./index.php //handler for the request in /
./blog/index.php //handler for any request to /blog
Each index.php expects to receive a ?path={request_uri} so that it can map the request to the controllers and actions.
I have tried multiple ways to get NGINX to do this using tryfiles and rewrite, but no luck. Using rewrite I can get / to work, but it wont render /mypage or /index/sitemap.
If I try to hit /index/sitemap it downloads the index.php instead of executing it, and if I try the blog the same thing happens. In fact the only path that works is /, all others just download the index.php file.
Here is my configuration as it is now, where am I going wrong?
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control “public”;
try_files $uri #fallback;
}
location / {
#index index.php index.html index.html;
rewrite ^/([^?]*) /index.php?path=$1 break;
rewrite ^blog/([^?]*) /blog/index.php?path=$1 break;
#try_files $uri #fallback;
}
Your configuration has multiple issues. I will ignore the first location block as it seems to have nothing to do with your question.
The first rewrite will always match, so the second rewrite will never be consulted. The second rewrite will never match anyway, as nginx URIs always begin with a /. The [^?] is meaningless, because rewrite uses a normalised URI which does not include the ? or query string. Using rewrite...break means that the rewritten URI is processed within the same location, which is an error as this location is not equipped to process PHP files. See this document for more.
A solution using try_files might look like this:
location / {
try_files $uri $uri/ /index.php?path=$uri&$args;
}
location /blog {
try_files $uri $uri/ /blog/index.php?path=$uri&$args;
}
location ~ \.php$ { ... }
See this document for more.
BACKGROUND
Our website has a blog subsection that runs off a simple custom-made CMS. I'm using Nginx as a webserver, PHP as the back-end language.
The blog index is /news, which contains dynamic links to each post. Clicking one of these links directs the user to /blog/post-title. I'm using Nginx to intercept any request to the post template page located in the /blog folder.
Here's the basic flow
1. User clicks blog with link to /blog/post-title
2. Nginx catches this request and serves it to /blog/index, which is our template
3. The post is displayed with the url https://www.domain.com/blog/post-title
My Problem
I would like to change the blog index from /news to /blog, but doing so causes anchors to direct to the file /blog rather than the folder /blog/.
Here is the current Nginx configuration
location /blog/ {
try_files $uri $uri/ /blog/index.php?$args;
}
TL;DR
I need nginx to catch and direct requests directed toward the folder /blog/ while ignoring the file /blog.php
This is a long shot as I'm not sure how the files are layout on your server but give this a try.
location ~ ^/blog/(.*)$ {
try_files $uri $uri/ /blog/index.php?$1;
}
I finished developed an app that features a downloading system that is hosted with NGINX at:
http://dashboard.myapp.com
The URL for downloads is:
http://dashboard.myapp.com/download/file-slug
This page is a regular PHP page that will require some user input and then PHP handles the actual file download, it's not the direct path for the file.
Since these download URLs will be made publicly available, I want to ditch that dashboard subdomain.
The default domain (myapp.com) is already working with a wordpress setup with this:
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Is there an easy way to get the:
http://myapp.com/download/file-slug
to act as if:
http://dashboard.myapp.com/download/file-slug
was accessed, without actually redirecting?
Try this - Place in your server block for myapp.com, anywhere outside another location block. Set the root to the same root as the dashboard subdomain (if on the same server). The script would see itself as being hosed at myapp.com instead of dashboard.myapp.com, but it should retain the remainder of the framework rules. If this doesn't work, try the next option.
location /download/file-slug {
root /path/folder;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Another option is to proxy through Nginx. This option actually runs the script on the current location, accessing it like a client would through dashboard.myapp.com. See proxy_pass documentation on Nginx.org.
location /download/file-slug { proxy_pass http://dashboard.myapp.com/download/file-slug; }
I was able to work it out with Nginx only.
Inside the myapp.com config file I added:
location ~ /download/(.*) {
resolver 8.8.8.8;
proxy_pass http://dashboard.myapp.com/download/$1;
}
The resolver 8.8.8.8 is actually using Google DNS. Without this line I was getting a "no resolver defined to resolve" error.
I'm in the midst of migrating over to Nginx, from Apache.
I'm currently using a custom content management solution that utilizes the SERVER['request_uri'] to handle routing.
What I am trying to do is redirect all non-existing files & directory requests to /index.php, and not update the clients uri. However, when a file does exist, I want to return that instead.
An example url would be:
localhost/content/page/1 <- Should populate $_SERVER['request_uri'] to be /content/page/1
Or
localhost/public/script/exists.js <- Should be returned as an actual file.
You need to add a location / block or update your current location / block in your nginx vhost file.
This will redirect all request to the index.php if the file or directory is not found:
location / {
try_files $uri $uri/ /index.php;
}
This goes inside your server directive, for more information visit http://wiki.nginx.org/HttpCoreModule
After you modify your vhost file you need to restart nginx
Note: The try_files directive for server blocks was added in 0.7.44
I have a wiki that hosts user-generated content with URLs like /wiki/view/pagename and /wiki/modify/pagename. I'm using an nginx configuration that goes something like:
location /wiki/ {
try_files $uri $uri/ /wiki/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi stuff...
}
It's been working great and as far as i can tell, this is the recommended approach. However, today, a user created a page named "whatever.php", so it needs the URLs /wiki/view/whatever.php to be redirected to my /wiki/index.php... but it gets caught in the second location block and returns a 404 to the user-agent.
Does anyone have any suggestions? Can i add an extra location block to rewrite *.php to the main script somewhere in such a way that won't affect actually routing pages? I still want to use nginx to serve static content inside the /wiki/ directory and to preserve the behaviour of everything outside this directory.
Repost of this dead forum thread