dongxiatuo9827 2016-04-17 23:33
浏览 57

将目录中的所有url重定向到nginx中的单个php

i want a single php script to handle all incoming requests to my /blog directoy. The php script checks wether a SQL record exists for the url (i.e. www.example.com/blog/example_article). If a record is found it serves the corresponding page data. If there is no record for a url (i.e. www.example.com/blog/nothing_here) it redirects to 404.

I need a rewrite rule in nginx for that.

My current config:

    root /usr/share/nginx/html/example/;
    index index.php;

    server_name example.com www.example.com;

    location / {
            try_files $uri $uri/ @extensionless-php;
    }

    location /blog {
            rewrite ?; // here i need a rewrite rule
    }

    location /uploads {
            deny all;
    }

    error_page 404 /templates/404.php;

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            fastcgi_intercept_errors on;
    }

    location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
    }

Please note that i am using extensionless URL's.

  • 写回答

1条回答 默认 最新

  • drq9991 2016-04-18 02:29
    关注

    Answer:

    location /blog {
        index article.php;
        rewrite ^/blog/(.*)$ /blog/article.php?slug=$1;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么