WenJunjie Blog

sysadmin

Pligg Nginx Rewrite

| Comments

Pligg 2 On Nginx Rewrite rules

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
server {
    listen       80;
    server_name  www.example.com;
    root   /usr/share/nginx/htdocs/example.com;
    index index.php  index.html index.htm;

    charset utf-8;

    error_page 404 502 = /404error.php;

    autoindex off;

    if ($uri !~ "(.[a-zA-Z0-9]{1,5}|/)$") {
         rewrite /(.*)([^/])$ http://$http_host/$1$2/ permanent;
    }
    if ($http_host !~ "^www.") {
         rewrite /.* http://www.$http_host$uri permanent;
    }


    location / {

    if (!-e $request_filename){
            ## Re-directing Begin
            # Story and Category URLs
            rewrite ^/(all)/([^/]+)/?$ /story.php?title=$2 last;
            rewrite ^/(all)/?$ index.php last;

            # /recent redirection
            rewrite ^/upcoming/recent/?$ /upcoming permanent;
            rewrite ^/upcoming/recent/category/([^/]+)/?$ /upcoming/category/$1 permanent;
            rewrite ^/upcoming/recent/page/(\d+)/?$ /upcoming/page/$1 permanent;
            rewrite ^/upcoming/recent/category/([^/]+)/page/(\d+)/?$ /upcoming/category/$1/page/$2 permanent;
            rewrite ^/recent/?$ /./ permanent;
            rewrite ^/recent/category/([^/]+)/?$ /category/$1 permanent;
            rewrite ^/recent/page/(\d+)/?$ /page/$1 permanent;
            rewrite ^/recent/category/([^/]+)/page/(\d+)/?$ /category/$1/page/$2 permanent;
            rewrite ^/upcoming/recent/([^/]+)/?$ /upcoming/category/$1 permanent;
            rewrite ^/upcoming/recent/([^/]+)/page/(\d+)/?$ /upcoming/category/$1/page/$2 permanent;
            rewrite ^/recent/([^/]+)/?$ /category/$1 permanent;
            rewrite ^/recent/([^/]+)/page/(\d+)/?$ /category/$1/page/$2 permanent;
            rewrite ^/upcoming/([^/]+)/rss/?$ /rss.php?status=queued&category=$1 last;
            rewrite ^/upcoming/rss/?$ /rss.php?status=queued last;
            rewrite ^/(\d+)/rss/?$ /storyrss.php?id=$1 last;
            rewrite ^/([^/]+)/rss/?$ /rss.php?category=$1 last;
            rewrite ^/user/profile/([^/]+)/rss/?$ /userrss.php?user=$1 last;
            rewrite ^/user/profile/([^/]+)/rss/([a-zA-Z0-9-]+)/?$ /userrss.php?user=$1&status=$2 last;
            rewrite ^/rss/?$ /rss.php last;
            rewrite ^/search/([^/]+)/rss/?$ /rss.php?search=$1 last;
            rewrite ^/group/([^/]+)/rss/?$ /rss.php?status=published&group=$1 last;
            rewrite ^/group/([^/]+)/upcoming/rss/?$ /rss.php?status=queued&group=$1 last;
            rewrite ^/group/([^/]+)/shared/rss/?$ /rss.php?status=shared&group=$1 last;
            rewrite ^/([^/]+)/([^/]+)/rss/?$ /storyrss.php?title=$2 last;
            rewrite ^/rss/([a-zA-Z0-9-]+)/?$ /rss.php?status=$1 last;
            rewrite ^/rss/category/([^/]+)/?$ /rss.php?category=$1 last;
            rewrite ^/rss/category/upcoming/([^/]+)/?$ /rss.php?status=queued&category=$1 last;
            rewrite ^/rss/category/published/([^/]+)/?$ /rss.php?status=published&category=$1 last;
            rewrite ^/rss/category/([^/]+)/queued/?$ /rss.php?status=queued&category=$1 last;
            rewrite ^/rss/category/([^/]+)/published/?$ /rss.php?status=published&category=$1 last;
            rewrite ^/rss/category/([^/]+)/group/([^/]+)/?$ /rss.php?category=$1&group=$2 last;
            rewrite ^/rss/category/upcoming/([^/]+)/([^/]+)/?$ /rss.php?status=queued&category=$1&group=$2 last;
            rewrite ^/rss/category/published/([^/]+)/([^/]+)/?$ /rss.php?status=published&category=$1&group=$2 last;
            rewrite ^/rss/search/([^/]+)/?$ /rss.php?search=$1 last;
            rewrite ^/rssfeeds/?$ /rssfeeds.php last;

            ## URL Method 2 Begin ##

            rewrite ^/?$ /index.php last;
            rewrite ^/advanced-search/?$ /advancedsearch.php last;
            rewrite ^/category/([^/]+)/?$ /index.php?category=$1 last;
            rewrite ^/category/([^/]+)/([^/]+)/?$ story.php?title=$2 last;
            rewrite ^/login/?$ /login.php last;
            rewrite ^/login/([a-zA-Z0-9-]+)/?$ /login.php?return=$1 last;
            rewrite ^/login/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /login.php?return=$1/$2 last;
            rewrite ^/logout/?$ /login.php?op=logout&return=index.php last;
            rewrite ^/logout/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /login.php?op=logout&return=$1/$2 last;
            rewrite ^/out/([\d]+)/?$ /out.php?id=$1 last;
            rewrite ^/out/(https?:.+)$ /out.php?url=$1 last;
            rewrite ^/out/([^/]+)/?$ /out.php?title=$1 last;
            rewrite ^/profile/?$ /profile.php last;
            rewrite ^/register/?$ /register.php last;
            rewrite ^/search/([^/]+)/page/(\d+)/?$ /search.php?search=$1&page=$2 last;
            rewrite ^/search/(.+)/?$ /search.php?search=$1 last;
            rewrite ^/searchurl/(.+)/?$ /search.php?url=$1 last;
            rewrite ^/settemplate/?$ /settemplate.php last;
            rewrite ^/story/([0-9]+)/?$ /story.php?id=$1 last;
            rewrite ^/story/([^/]+)/?$ /story.php?title=$1 last;
            rewrite ^/story/([0-9]+)/editcomment/([0-9]+)/?$ /edit.php?id=$1&commentid=$2 last;
            rewrite ^/story/([0-9]+)/edit/?$ /editlink.php?id=$1 last;
            rewrite ^/submit/?$ /submit.php last;
            rewrite ^/tag/([^/]+)/([^/]+)/?$ /search.php?search=$1&tag=true&from=$2 last;
            rewrite ^/tag/([^/]+)/?$ /search.php?search=$1&tag=true last;
            rewrite ^/tag/([^/]+)/page/(.+)/?$ /search.php?search=$1&tag=true&page=$2 last;
            rewrite ^/tagcloud/?$ /cloud.php last;
            rewrite ^/tagcloud/range/([0-9]+)/?$ /cloud.php?range=$1 last;
            rewrite ^/topusers/?$ /topusers.php last;
            rewrite ^/trackback/([0-9]+)/?$ /trackback.php?id=$1 last;
            rewrite ^/upcoming/?$ /upcoming.php last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/?$ /upcoming.php?part=$1 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/?$ /upcoming.php?part=$1&category=$2 last;
            rewrite ^/upcoming/([^/]+)/?$ /upcoming.php?category=$1 last;
            rewrite ^/upcoming/page/([0-9]+)/?$ /upcoming.php?page=$1 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/page/(\d+)/?$ /upcoming.php?part=$1&page=$2 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/page/(\d+)/?$ /upcoming.php?part=$1&category=$2&page=$3 last;
            rewrite ^/user/?$ /user.php last;
            rewrite ^/user/search/([^/]+)/?$ /user.php?view=search&keyword=$1 last;
            rewrite ^/user/profile/([^/]+)/following/?$ /user.php?login=$1&view=following last;
            rewrite ^/user/profile/([^/]+)/followers/?$ /user.php?login=$1&view=followers last;
            rewrite ^/user/([^/]+)/?$ /user.php?view=$1 last;
            rewrite ^/user/([^/]+)/([^/]+)/?$ /user.php?view=$1&login=$2 last;
            rewrite ^/user/([^/]+)/link/([0-9+]+)/?$ /user_add_remove_links.php?action=$1&link=$2 last;

            ## Admin
            rewrite ^/admin/?$ /admin/admin_index.php last;
            rewrite ^/admin_comments/page/([^/]+)/?$ /admin/admin_comments.php?page=$1 last;
            rewrite ^/admin_links/page/([^/]+)/?$ /admin/admin_links.php?page=$1 last;
            rewrite ^/admin_users/page/([^/]+)/?$ /admin/admin_users.php?page=$1 last;
            rewrite ^/story/([0-9]+)/modify/([a-z]+)/?$ /admin/linkadmin.php?id=$1&action=$2 last;
            rewrite ^/view/([^/]+)/?$ /admin/admin_users.php?mode=view&user=$1 last;

            ## Groups
            rewrite ^/join_group/action/(published|queued|discard)/link/(\d+)/?$ /join_group.php?action=$1&link=$2 last;
            rewrite ^/groups/?$ /groups.php last;
            rewrite ^/groups/search/([^/]+)/?$ /groups.php?view=search&keyword=$1 last;
            rewrite ^/groups/submit/?$ /submit_groups.php last;
            rewrite ^/groups/(members|name|oldest|newest)/?$ /groups.php?sortby=$1 last;
            rewrite ^/groups/([^/]+)/?$ /group_story.php?title=$1 last;
            rewrite ^/groups/([^/]+)/page/([0-9]+)/?$ /group_story.php?title=$1&page=$2 last;
            rewrite ^/groups/([^/]+)/?$ /group_story.php?title=$1&view=published last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/?$ /group_story.php?title=$1&view=$2 last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/page/([0-9]+)/?$ /group_story.php?title=$1&view=$2&page=$3 last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/category/([^/]+)/?$ /group_story.php?title=$1&view=$2&category=$3 last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/category/([^/]+)/page/([0-9]+)?$ /group_story.php?title=$1&view=$2&category=$3&page=$4 last;
            rewrite ^/groups/delete/([0-9]+)/?$ /deletegroup.php?id=$1 last;
            rewrite ^/groups/edit/([0-9]+)/?$ /editgroup.php?id=$1 last;
            rewrite ^/groups/id/([0-9]+)/?$ /group_story.php?id=$1 last;
            rewrite ^/groups/join/([0-9]+)/? /join_group.php?id=$1&join=true last;
            rewrite ^/groups/member/admin/id/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=admin&userid=$3 last;
            rewrite ^/groups/member/normal/id/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=normal&userid=$3 last;
            rewrite ^/groups/member/moderator/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=$2&userid=$3 last;
            rewrite ^/groups/member/flagged/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=flagged&userid=$3 last;
            rewrite ^/groups/member/banned/id/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=banned&userid=$3 last;
            rewrite ^/groups/page/([0-9]+)/?$ /groups.php?page=$1 last;
            rewrite ^/groups/unjoin/([0-9]+)/? /join_group.php?id=$1&join=false last;
            rewrite ^/groups/withdraw/([0-9]+)/user_id/([0-9]+)/?$ /join_group.php?group_id=$1&user_id=$2&activate=withdraw last;
           
            ## Live
            rewrite ^/live/?$ /live.php last;
            rewrite ^/live/comments/?$ /live_comments.php last;
            rewrite ^/live/comments/page/([^/]+)/?$ /live_comments.php?page=$1 last;
            rewrite ^/live/published/?$ /live_published.php last;
            rewrite ^/live/published/page/([^/]+)/?$ /live_published.php?page=$1 last;
            rewrite ^/live/upcoming/?$ /live_unpublished.php last;
            rewrite ^/live/upcoming/page/([^/]+)/?$ /live_unpublished.php?page=$1 last;

            #Modules
            rewrite ^/inbox/?$ /module.php?module=simple_messaging&view=inbox last;
            rewrite ^/sitemapindex.xml /module.php?module=xml_sitemaps_show_sitemap last;
            rewrite ^/sitemap-([a-zA-Z0-9]+).xml /module.php?module=xml_sitemaps_show_sitemap&i=$1 last;
            rewrite ^/status/([0-9]+)/?$ /modules/status/status.php?id=$1 last;
            rewrite ^/toolbar/(\d+)/?$ /modules/pligg_web_toolbar/toolbar.php?id=$1;
            rewrite ^/sitemapindex.xml /module.php?module=xml_sitemaps_show_sitemap last;
            rewrite ^/sitemap-([0-9a-z]+).xml /module.php?module=xml_sitemaps_show_sitemap&i=$1 last;
     
            ## Pages
            rewrite ^/about/?$ /page.php?page=about last;
            rewrite ^/static/([^/]+)/?$ /page.php?page=$1 last;

            ## Pagination
            rewrite ^/category/([^/]+)/page/([^/]+)/?$ /index.php?category=$1&page=$2 last;
            rewrite ^/page/([^/]+)/?$ /index.php?page=$1 last;
            rewrite ^/page/([^/]+)/([^/]+)category/([^/]+)/?$ /index.php?page=$1&part=$2&category=$3 last;
            rewrite ^/upcoming/([^/]+)/page/(\d+)/?$ /upcoming.php?category=$1&page=$2 last;
            rewrite ^/published/page/([^/]+)/([^/]+)/?$ /index.php?page=$1&category=$2 last;
            rewrite ^/published/page/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1&part=$2&category=$3 last;
            rewrite ^/published/page/([^/]+)/?$ /index.php?page=$1 last;
            rewrite ^/published/page/([^/]+)/category/([^/]+)/?$ /index.php?page=$1&category=$2 last;
            rewrite ^/published/page/([^/]+)/([^/]+)category/([^/]+)/?$ /index.php?page=$1&part=$2&category=$3 last;
            rewrite ^/published/page/([^/]+)/([^/]+)/?$ /index.php?page=$1&part=$2 last;
            rewrite ^/published/page/([^/]+)/range/([^/]+)/?$ /?page=$1&range=$2 last;
            rewrite ^/search/page/([^/]+)/([^/]+)/?$ /search.php?page=$1&search=$2 last;
            rewrite ^/topusers/page/([^/]+)/?$ /topusers.php?page=$1 last;
            rewrite ^/topusers/page/([^/]+)/sortby/([^/]+)?$ /topusers.php?page=$1&sortby=$2 last;
            rewrite ^/user/page/([^/]+)/([^/]+)/([^/]+)/?$ /user.php?page=$1&view=$2&login=$3 last;
            rewrite ^/user/([^/]+)/([^/]+)/page/(\d+)/?$ /user.php?page=$3&view=$1&login=$2 last;
           
            ## Sort
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/?$ /index.php?part=$1 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/?$ /index.php?part=$1&category=$2 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/page/(\d+)/?$ /index.php?part=$1&page=$2 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/page/(\d+)/?$ /index.php?part=$1&category=$2&page=$3 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/?$ /upcoming.php?part=$1&category=$2 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/page/(\d+)/?$ /upcoming.php?part=$1&category=$2&page=$3 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/?$ /index.php?part=$1&category=$2 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/page/(\d+)/?$ /index.php?part=$1&category=$2&page=$3 last;

            ## URL Method 2 End ##
    }

    if (!-e $request_filename){
            rewrite ^/([^/]+)/?$ /index.php?category=$1 last;
    }
    if (!-e $request_filename){
            rewrite ^/([^/]+)/page/([^/]+)/?$ /index.php?category=$1&page=$2 last;
    }
    if (!-e $request_filename){
            rewrite ^/([^/]+)/([^/]+)/?$ /story.php?title=$2&category=$1 last;
            rewrite ^/([^/]+)/([^/]+)/([0-9]+)/?$ /story.php?title=$2&category=$1&comment_id=$3 last;
            rewrite ^/([^/]+)/([^/]+)/reply/([0-9]+)/?$ /story.php?title=$2&category=$1&comment_id=$3&reply=1 last;
    }

    if ($query_string ~* 'mosConfig_[a-zA-Z_]{1,21}(=|\%3D)' ){
     return 405;
    }
    if ($query_string ~* base64_encode.*\(.*\) ){
     return 405;
    }
    if ($query_string ~* (\<|%3C).*script.*(\>|%3E) ){
     return 405;
    }
    if ($query_string ~* 'GLOBALS(=|\[|\%[0-9A-Z]{0,2})' ){
     return 405;
    }
    if ($query_string ~* '_REQUEST(=|\[|\%[0-9A-Z]{0,2})' ){
     return 405;
    }

    if ($http_user_agent ~* ^pycurl/){
            return 405;
    }

}

    location ~ \.php$ {
        root           /usr/share/nginx/htdocs/example.com;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location /settings.php {
        deny all;
    }
    location ~ /\.(tpl|log)$ {
        deny all;
    }
}

Comments