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 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM