skip to Main Content

apache .htaccess

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{HTTP:Authorization} ^(.+)$
  RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]


  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>

Please help me. This problem has bothered me for 3 days. Thank you very much

I try to convert it into the following

if (!-e $request_filename){
    rewrite ^/(.*)$  /index.php/$1  last;
}

Authorization cannot be obtained correctly on nginx,Can cause CORS problems

I think it’s because this sentence hasn’t been converted successfully

  RewriteCond %{HTTP:Authorization} ^(.+)$
  RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

because I don’t know how to convert it

Please help me

I expect to get the converted nginx htaccess

2

Answers


  1. Chosen as BEST ANSWER

    This is the answer

    if (!-f $request_filename) {
        rewrite ^(.*)$ /index.php?s=/$1 last;
        break;
    }
    
    if (!-d $request_filename) {
        rewrite ^(.*)$ /index.php?s=/$1 last;
        break;
    }
    

  2. The .htaccess you’re seeking to convert is quite standard "front controller" pattern. What it does is, for any URI, it will attempt to:

    • serve the file matching the exact URI
    • if the file wasn’t found in the previous step, then check a directory, and attempt to serve from there (subject to index directives or further rewrites)
    • if the directory wasn’t found, route the request to index.php

    The de-facto standard NGINX snippet that does the same, utilizes try_files directive.

    You’ll find this used for virtually all PHP-based CMS configs for NGINX:

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    

    The try_files directive, similar to if when being checked for file existence, has its performance pitfalls, so in fact, you may want to do as the article says is route unconditionally all request via PHP-FPM by default, then some (e.g. static assets) served by NGINX directly. Example bare-bones config for WordPress which illustrates the idea:

    index index.html index.php;
    location / {
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        include fastcgi_params;
        # override SCRIPT_NAME which was set in fastcgi_params
        fastcgi_param SCRIPT_NAME /index.php;
        fastcgi_pass unix:/var/run/php-fpm/example.com.sock;
    }
    location /wp-content/ { }
    

    In this case /wp-content/ is allocated as an empty location in order to ensure NGINX does not pass requests to files in it via PHP-FPM. It will serve everything directly for static files.

    Thus the URL rewriting is actually eliminated, and all the performance penalty that comes with it is negated.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search