如何使用htacces在nginx的子文件夹中运行index.php?

时间:2016-02-05 17:08:47

标签: php apache .htaccess nginx

我是 nginx 的新手。

我使用 nginx 运行我的网站。

我已尝试将result.find_next_sibling("ul", class="line3")转换为使用htaccessnginx.conf,但我的网站仍无效。

以下是我的文件:

default.d/*.conf

首先是htaccess

--- .htaccess ---
 |-- public     |
 |     ---  index.php
 |     --- .htaccess
 |-- application

第二个htaccess RewriteEngine on RewriteRule ^(.*) public/$1 [L]

相同的文件夹
index.php

编辑后我的Options -MultiViews RewriteEngine On Options -Indexes RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

nginx.conf

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; autoindex off; location / { # first htaccess configuration rewrite .* /public/index.php last; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

default.d/*.conf

任何人都可以帮我在nginx中运行吗?

1 个答案:

答案 0 :(得分:1)

一种选择是将.../public设置为文档根目录,并将以/public/开头的任何URI重写为/

root /usr/share/nginx/html/public;

location / {
    try_files $uri @index;
}
location @index {
    rewrite ^/(.*)$ /index.php?url=$1 last;
}
location /public/ {
    rewrite ^/public(.*)$ $1 last;
}
location ~* \.php$ {
    try_files $uri =404;
    ...
}

但是,如果您更喜欢使用当前的文档根目录,则可能符合您的要求:

root /usr/share/nginx/html;

location / {
    try_files $uri /public$uri @index;
}
location @index {
    rewrite ^/(.*)$ /index.php?url=$1 last;
}
location ~* \.php$ {
    try_files $uri /public$uri =404;
    ...
}