I have the following route in laravel 5.2
Route::get('images/{filename}', function ($filename)
{
if($filename)
{
return redirect('/home');
}
});
If a person enters the following url: www.mydomain.com/images/image_abc123.jpg, I hope that it would be redirected to www.mydomain.com/home. But the route is not triggering and goes to show the image instead.
Any advise? Thank you!
Edit: here is my .htaccess the default of laravel 5.2 under /public directory
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
This is probably because of your webserver. The common default is typically like this.
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name cards.foo *.cards.foo;
root /var/www/cards/public;
index index.php;
include php.conf;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
Notice the try_files directive. This is telling the webserver:
If the request is an extant file, open it.
If the request is an extant directory, index it.
Else, pass the directory as an argument to the /index.php.
So, on the first pass, "Does that file exist?", it says "yes" and bypasses PHP 100%. You will want to change that.
alright! public folder is named public for a reason!
to fix this you have to put those images in the storage folder (thats what the storage folder is if you ever wondered)
Storage/app
so they dont exist in public folder anymore and people can't reach them unless
you do this
Route::get('images/{filename}', function ($filename)
{
// get the image named $slug from storage and display it
// Something like (not sure)
$image = File::get('images/' . $filename . '.jpg');
return response()->make($image, 200, ['content-type' => 'image/jpg']);
});
what you want to achieve = preventing some or all people from accesing a file
what the answer does =prevents the files from being public unless you serve theme using a method
i hope everything is clear
Related
I'm trying to migrate a site under CodeIgniter from Apache to Nginx. My first tests show very good performances, so it worst the try.
But I can't find the correct Nginx configuration to replace those RewriteRules :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I found this recipe on Nginx website https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
location / {
try_files $uri $uri/ /index.php;
}
It works in most cases, except when I have a trailing slash. Meaning
http://example.com/controller/param/
will call http://example.com/controller/function/index.php (and return a 404)
instead of http://example.com/index.php/controller/function/ ...as it usually do with Apache rewrite.
I tried to add a rewrite :
rewrite ^/(.*)/$ /$1 permanent;
But it redirect POST to GET, so I loose all my post data...
Any clue ?
Thanks
Check if this helps. Replace both occurrences of <source_directory>; with your correct value:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root <source_directory>;
rewrite_log on;
index index.html index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.htaccess
{
deny all;
}
error_page 404 /404.html;
location = /40x.html
{
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
}
}
Sorry about my english. I was searching about 2 days without answers.
the problem is:
i need to run a cron job. I write in console from ssh in godaddy this:
/web/cgi-bin/php5 /path/to/my/project/html/www/myproject/index.php crons test
and i get:
Status: 301 Moved Permanently
Location: http://www.
Content-type: text/html
i have in .../application/controllers the file crons.php which have this code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Crons extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
echo "hello word";
}
}
/* End of file crons.php */
/* Location: ./application/controllers/crons.php */
it is going to my index.php but it is not working because in my index.php i have this code for redirect .mysite to www.mysite, and $getsite_all is the error:
$getsite_all = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$getsite = $_SERVER['HTTP_HOST'];
$getsite = str_replace($e_dom, '', $getsite);
$ugetsite = explode('.', $getsite);
if ($ugetsite[0] != 'www') {
$estimated_url = 'http://www.' . $getsite_all;
header('HTTP/1.1 301 Moved Permanently');
header("Location: $estimated_url");
die();
}
but if i delete this lines the error is from that lines:
// Is the system path correct?
if (!is_dir($system_path)) {
exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: " . pathinfo(__FILE__, PATHINFO_BASENAME));
}
so i think that the error can come from my .htaccess which is:
<FilesMatch "\.(css|js|gif|jpe?g|png|ico)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</FilesMatch>
FileETag none
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Impide el acceso a la carpeta del sistema
# "system" se debe cambiar si el nombre la carpeta se modifica.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Esto impide el acceso a la carpeta de la aplicación
# 'application' se debe cambiar si el nombre la carpeta se modifica.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)/$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
# Comprueba si el usuario está intentando acceder a un archivo válido,
# como un documento de imagen o CSS, si esto no es cierto
# se redirecciona a index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|css|js|images|robots.txt)
RewriteRule ^(.*)$ /index.php?/$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
i think the error can be in:
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
but my web is running perfectly and i dont know if delete this lines.
What i must do?
Thanks!
ok it was very simple. in godaddy you can put directly:
wget http://www.example.com/mycontroller/mymethod
in my example:
wget http://www.example.com/crons/test
and it works perfectly. what i cant understand is why goddady dont put that simple information in his cron job panel.
I have 2 website. Let's say website A and B.
Both are using Magento.
Almost all of products in B also built-in in A.
Website B is going to be closed hence all of its matching products will be redirected to website A.
For other page (non-matching products & other url) will be redirected to A's homepage.
I created script to map matching products from B to A (put it under name mapping.txt), it contains around 20000 url.
To do the redirection, I'm using nginx HttpMapModule (http://wiki.nginx.org/HttpMapModule)
I was able to map it by using this conf:
map $uri $new{
default http://www.a.com/?ref=b;
include /some/path/mapping.txt;
}
server {
listen 80 ;
listen 443 ssl;
server_name www.b.com;
root /some/path/www/b;
#redirect b
rewrite ^ $new redirect;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
.
.
.
}
The problem is, I want to be able to access B's admin page.
Because of the default map, I can not access http://www.b.com/admin anymore.
Then, I changed the conf:
map $uri $new{
default http://www.a.com/?ref=b;
include /some/path/mapping.txt;
}
server {
listen 80 ;
listen 443 ssl;
server_name www.b.com;
root /some/path/www/b;
location / {
#redirect b
rewrite ^ $new redirect;
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
.
.
.
}
By this conf, I still unable to access http://www.b.com/admin but I can access http://www.b.com/index.php/admin but the css and js are not loaded because request to (http://www.b.com/skin/..... and http://www.b.com/js/..... are redirected to http://www.a.com/?ref=b)
But this conf is also problematic, it won't redirect to A's homepage if I type http://www.b.com/index.php/customer/account/login
example content of mapping.txt:
/product-abc.html http://www.a.com/product-abc12.html;
/category-a/product-xyz.html http://www.a.com/product-xyz.html;
/category-b/category-d/product-123.html http://www.a.com/category-e/product-12z.html;
You can use a simple domain redirect on .htaccess for these generic cases...
RewriteCond %{HTTP_HOST} ^www.a.com$ [NC]
RewriteRule ^(.*)$ http://www.b.com/$1 [R=301,L]
Can anyone help me with this?
RewriteCond %{REQUEST_URI} !^\/index\.php$
RewriteCond %{REQUEST_URI} !^\/search\/.*
RewriteCond %{REQUEST_URI} ^\/([^/]*)\.php$
RewriteRule (.*) http://www.mydomain.com/index.php?page=$1
I need to convert this for nginx
I tried this, but it won't work:
if ($request_uri ~* !^\/search\/.*) {
if ($request_uri ~* !^\/index\.php$) {
if ($request_uri ~* ^\/([^/]*)\.php$) {
rewrite (.*) http://www.mydomain.com/index.php?page=$1;
}
}
}
I ended up using:
if ($uri !~ "^/index.php$") {
set $rule_0 1$rule_0;
}
if ($uri !~ "^/search/.*") {
set $rule_0 2$rule_0;
}
if ($uri ~ "^/([^/]*).php$") {
set $rule_0 3$rule_0;
}
if ($rule_0 = "321") {
rewrite ^(.*) /index.php?page=$1 last;
}
Try changing the ~* ! to !~*, and combine the last 2:
if ($request_uri !~* ^/search/.*) {
if ($request_uri !~* ^/index\.php$) {
rewrite ^/([^/]*)\.php$ http://www.mydomain.com/index.php?page=$1 redirect;
}
}
Or stick with location based rules:
location ~ /index\.php$ { }
location ~ /search/.* { }
location / {
rewrite ^/([^/]*)\.php$ http://www.mydomain.com/index.php?page=$1 redirect;
}
How can I 301 rewrite mysite.com/page/ to mysite.com/page/index.html using nginx?
In Apache I had:
RewriteRule page/ http://mysite.com/page/index.html [R=301,L]
Thanks for help,
hydn
Try this settings:
location / {
rewrite /page/ http://mysite.com/page/index.html permanent;
...
}
I see from your comment to Sergiei that the '/page/' directory and '/page/index.html' does not actually exist and is rewritten elsewhere. So not surprising that Nginx gives a 404 not found.
What exactly should get served if a visitor requests '/page/index.html'? I.E., what does that get rewritten to?
If it is index.php?q=/page/index.html, then your config should be:
server {
# index directive should be in server or http block
# So no need to repeat in every location block
# unless specifically overriding it
index index.php index.html;
location / {
rewrite ^/page(/?)$ /page/index.html break;
try_files $uri $uri/ /index.php?q=$uri;
}
}
You could also use
server {
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
}
But there may be some issues with that. All depends on the detail of your application.