Nginx - How to redirect a directory to a file that its path contains the directory? - redirect

I'm new to Nginx and I'm trying to redirect a directory to a file, here is basically what I'm trying to do:
When entering this link:
http://localhost:8889/dir
go to this link instead:
http://localhost:8889/dir/path/to/the/file/index.html
As you can see the directory is part of the file path which I'm trying to redirect to. Plus there is already a location block in the config file to redirect that directory to the proper location:
location /dir {
root /opt/dir;
}
My first attempt was to use the rewrite directive as I saw from this blog(https://jeffsebring.com/2012/nginx-301-redirects/):
if ( $request_filename ~ dir ) {
rewrite ^ http://localhost:8889/dir/path/to/the/file/index.html permanent;
}
But the page says it has a redirect loop, which I believe is the conflict with the old location block.
Then I tried to add another location block as I saw from here(nginx rewrite virtual directory to file):
location /dir {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
But after reloading config file, Nginx told me there is already a location block with the same directory exists.
So my question is is there any way I can do this? or it is not even possible?
Thanks!!

Found the solution.
I used "return" to return the full hard-coded url instead rewrite the current one:
location ~* /dir$ {
return http://localhost:8889/dir/path/to/the/file/index.html;
}
And also this rewrite solution works as well:
location ~* /dir$ {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
The problem I had was actually caused by cache. I was actually reloading the page from cache so I could not see the result after changing nginx config file, and I resolved this by checking the "Disable cache" option in developer tools in Chrome.

Related

Nginx folder redirection without changing the url

I want to redirect an old path to a new one without changing the url and keeping the suffix of the url for the redirection:
dev-osm.blah.com/NYC/v2/site/links?key=a12345
redirect to
dev-osm.blah.com/NYC/v1/site/links2?key=a12345
server {
server_name dev-osm.blah.com
.
.
location ^~ /NYC/v2/site/links {
rewrite ^ /NYC/v1/site/links2
}
Use something like the following in your server config
location /NYC/v2
proxy_pass: /NYC/v1/

How to run custom php script from doc root in magento2

I wanted to add custom php script to magento2 root folder and run from the browser.I tried to add it in magento2 root folder but it redirects to 404 page.
I also tried to add it in pub folder but no success.
Also cleared cache and empty generation folder.
I am using nginx server
If you are using nginx configuration that comes with magento you need to put a file inside pub folder to allow access to it from the browser as pub is the document root of the vhost. Magento root dir is one level up. Second of all default config for nginx allows only to access index.php, get.php, static.php, report.php, 404.php and 503.php files. Any other are not processed by the php. You can see this in line with location ~ (index|get|static|report|404|503)\.php$ { in nginx.conf.sample. If you are not using it check your config for similar rule. To allow another file to be accessible from browser simple add another name after 503 or change entire brackets with location ~* \.php$ {
Source: https://magento.stackexchange.com/a/97290/1883
For example you can get product name in custom script by this step
step 1: create index.php at root of magento 2
magento2/test/index.php
<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
step 2: create customScript.php
magento2/test/customScript.php
<?php
class customScript
extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
$id = 12;
$_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
echo $_product->getName();
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
Now you can run this custom script by
http://10.16.16.196/magento2/test/
As stated by #Ranjit, the /pub folder must be your Magento root folder.
The correct way to run a standalone php script on Magento would be:
On nginx:
Locate location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ { and add your file there.
I.e:
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|myphp)\.php$ {
Then you can access yourstore.com/myphp.php.
On Apache:
Simply add the file under /pub folder. I.e.: /pub/myphp.php.
Apache rewrites rule will redirect to index.php if the file or folder doesn't exist.
In my case on Apache (cPanel) the problem was that file permissions of .php files should not be writable by group or others to be served directly, else Magento's 404 would open.
So in my case to serve file directly I had to set file permission to -rw-r--r-- (on Linux).
This is the mostly issue with servers using suPHP.
Putting this here in case someone runs into same situation...

How to change the Public directory path in Lumen?

By default Lumen "same as Laravel" has myApp/public directory to put all the public files (assets).
I want to change that directory path from myApp/public to myApp/src/custom/public. How can I do achieve this?
I also struggled with this and found a soltuion elsewhere.
I wanted the following directory structure:
.hosting root dir
├── lumen_app_dir <---
├── other_app_dir
├── etc...
└── public_html
└──lumen_app_public_dir <---
So I did the following:
Copied the index.php and .htaccess from lumen_app_dir/public to the lumen_app_public_dir.
Changed the index.php there like this:
$app = require __DIR__.'/../../lumen_app_dir/bootstrap/app.php';
$app->run($app['request']);
The important part here is, that I had to include $app['reqest'] as the parameter for run function.
And it just works without any change in the default .htaccess file. I can access the Lumen installation at server.dev/lumen_app_public_dir
Got the same problem, and solved it. Look here, maybe it will help you.
I have done this solution for my Lumen application, which works for me.
UPDATE
Ok, let's go proceeding some changes to make the system to work with your tree.
Add a .htaccess file in the root of your application so in the directory myApp\. Write it in :
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ src/custom/public/$1 [L,NC]
Assuming that you have configured your vhost pointing to \Path\myApp, we now accede to the index file of myApp\src\custom\public\. If we didn't make any mistakes, then we should get to a page that indicates an error, telling us that the bootstrap/app.php file is not found. Logic.
We must therefore change the index.php file in the directory myApp\src\custom\public :
Change from this :
$app = require __DIR__.'/../bootstrap/app.php';
To this :
$app = require __DIR__.'/../../../bootstrap/app.php';
You can now get your home page directly from the path wanted.
You can override your public directory using the IoC container like this:
App::bind('path.public', function()
{
return base_path().'/public_html';
});
But I prefer to use a symlink to the public folder like this:
ln -s public public_html
I believe you should "point" your vhost to the new location (assuming you moved/renamed the public dir to your desired path). This is only needed is your server is not yet configured this way.
Then open the index.php from your new location and "fix" the path to bootsrap/app.php
Then open server.php from the Lumen base dir and edit your paths in both locations you find "public" mentioned.
Didn't really test this but looks like it should work. Give it a try.
One solution is to bind the path fro public in the appServiceProvider.php register method:
$app->bind('path.public', function() {
return __DIR__; });
A reliable way is to go into your public_html/index.php!
I'm using Lumen 6.x and it's work properly.

PHP Built-in Webserver and Relative Paths

TL;DR
Does PHP 5.4 built-in webserver have any bug or restriction about relative paths? Or does it need to be properly (and additionally) configured?
When I used to programming actively I had a system working under URI routing using these lines in a .htaccess file:
RewriteEngine On
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [L]
The FrontController received the Request, find the proper route from given URI in a SQLITE database and the Dispatcher call the the Action Controller.
It worked very nicely with Apache. Today, several months later I decided to run my Test Application with PHP 5.4 built-in webserver.
First thing I noticed, obviously, .htaccess don't work so I used code file instead:
<?php
if( preg_match( '/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"] ) ) {
return false;
}
include __DIR__ . '/index.php';
And started the webserver like this:
php.exe -c "php.ini" -S "localhost:8080" "path\to\testfolder\routing.php"
So far, so good. Everything my application need to bootstrap could be accomplished by modifying the include_path like this:
set_include_path(
'.' . PATH_SEPARATOR . realpath( '../common/next' )
);
Being next the core folder of all modules inside a folder for with everything common to all applications I have. And it doesn't need any further explanation for this purpose.
None of the AutoLoader techniques I've ever saw was able to autoload themselves, so the only class manually required is my Autoloader. But after running the Test Application I received an error because my AutoLoader could not be found. o.O
I always was very suspicious about realpath() so I decided to change it with the full and absolute path of this next directory and it worked. It shouldn't be needed to do as I did, but it worked.
My autoloader was loaded and successfully registered by spl_autoload_register(). For the reference, this is the autoloading function (only the Closure, of course):
function( $classname ) {
$classname = stream_resolve_include_path(
str_replace( '\\', DIRECTORY_SEPARATOR, $classname ) . '.php'
);
if( $classname !== FALSE ) {
include $classname;
}
};
However, resources located whithin index.php path, like the MVC classes, could not be found. So I did something else I also should not be doing and added the working directory to the include_path. And again, manually, without rely on realpath():
set_include_path(
'.' . PATH_SEPARATOR . 'path/to/common/next'
. PATH_SEPARATOR . 'path/to/htdocs/testfolder/'
);
And it worked again... Almost! >.<
The most of Applications I can create with this system works quite well with my Standard Router, based on SQLITE databases. And to make things even easier this Router looks for a predefined SQLITE file within the working directory.
Of course, I also provide a way to change this default entry just in case and because of this I check if this file exist and trigger an error if it doesn't.
And this is the specific error I'm seeing. The checking routine is like this:
if( ! file_exists( $this -> options -> dbPath ) ) {
throw RouterException::connectionFailure(
'Routes Database File %s doesn\'t exist in Data Directory',
array( $this -> options -> dbPath )
);
}
The dbPath entry, if not changed, uses a constant value Data/Routes.sqlite, relatively to working directory.
If, again, again, I set the absolute path manually, everything (really) works, the the Request flow reached the Action Controllers successfully.
What's going on?
This a bug in PHP's built-in web server that is still not fixed, as of PHP version 5.6.30.
In short, the web server does not redirect to www.foo.com/bar/ if www.foo./bar was requested and happens to be a directory. The client being server www.foo.com/bar, assumes it is a file (because of the missing slash at the end), so all subsequent relative links will be fetched relative to www.foo.com/instead of www.foo.com/bar/.
A bug ticket was opened back in 2013 but was mistakenly set to a status of "Not a Bug".
I'm experiencing a similar issue in 2017, so I left a comment on the bug ticket.
Edit : Just noticed that #jens-a-koch opened the ticket I linked to. I was not awar of his comment on the original question.

Nginx location, alias, rewrite, root

I'm serving /foo/bar/ by way of proxypass and want to continue doing so. However, I would like to serve /foo/bar/baz.swf statically from say /var/www/mystatic/baz.swf and so forth.
I was hoping that I could do something like
location /foo/bar/(.*) {
alias /var/www/mystatic/;
}
location / {
proxy_pass ....;
...
}
And /foo/bar/ would go to the application server while /foo/bar/(.*) would be served statically.
the docs say that I can't do this and need to use a combination of root and rewrite: http://wiki.nginx.org/NginxHttpCoreModule
Adding to the complication, I would like to continue using the ancient, unsupported 0.5.33. Any help would b greatly appreciated.
Edit: moving forward, someone suggested using root instead of alias. But, it doesn't seem that I can use any regex on the location directive with my version? Here, /foo/bar/baz.swf is served by the proxy_pass! I have the file at /var/www/foo/bar/baz.swf.
location /foo/bar/(.+) {
root /var/www/;
}
You can do this; but it's slightly esoteric. Try using:
location ^~ /foo/bar {
alias /var/www/mystatic/;
}
location / {
proxy_pass ....;
}
These options are documented on the Wiki http://wiki.nginx.org/NginxHttpCoreModule#location
location = /foo/bar/baz.swf {}
will clear clear any options set to /foo/bar/baz.swf. So you can leave it
where it is as the proxy options will not be used.
You can:
# mkdir /var/www/foo
# mv /var/www/mystatic /var/www/foo/bar
then use this config:
location ~ ^/foo/bar/(.+) {
root /var/www/;
}