简体   繁体   中英

Re-work Apache .htaccess to nginx configuration

I'm trying rewrite my nginx to use http://domain.com/main instead of using http://domain.com/?page=main

I've done:

try_files $uri $uri/ /index.php?page=$uri;

Returns blank page

rewrite ^/(\w+)$ /index.php?page=$1 break;
rewrite ^/(\w+)+\/$ /index.php?page=$1 break;
if ($http_host !~ "^$"){
   rewrite ^(.*)$ http%1://www.$http_host$request_uri redirect;
}

Generates the url: http://domain.com/main/http://domain.com/main/http://domain.com/main

Here is the Apache .htaccess:

RewriteEngine on
RewriteRule ^(\w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(\w+)+\/$ index.php?page=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I am assuming that you have a working configuration for URIs of the form: http://domain.com/?page=main

The try_files $uri $uri/ /index.php?page=$uri; may not work as expected as it will generate:

http://domain.com/?page=/main

Note the extra / . So you may need to use a rewrite to extract part of the URI not including the leading / . For example:

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

Note the last suffix rather than the break suffix, as the target URI needs to be processed in another location. See this document for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM