正确的重写语法以删除 index.php

正确的重写语法以删除 index.php

我有一个 nginx vHost,它托管:

  • CMS/
  • 一个 magento 商店/store

一切都很顺利,除了一件事:

从 URL 中删除 index.php。目前,以下 URL 有效

example.com/store/index.php/my-funny-test-product.html and (as there are a few subshops in magento) 
example.com/store/index.php/city/my-funny-test-product.html

现在我必须建立一个重定向,以便可以从 URL 中剥离 index.php。

example.com/store/my-funny-test-product.html or example.com/store/city/my-funny-test-product.html

我使用这个 .htaccess 在本地 Apache 上使用它

RewriteEngine on
RewriteBase /store/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule .* index.php [L]

基于此,我构建了以下重写

location /store {
   rewrite ^/store /store/index.php;
}

现在example.com/store/my-funny-test-product.html可以工作了,但是 CSS 图像等都坏了!我尝试添加if (!-e $request_filename)来修复它,但随后出现了 nginx 40x 错误。

如何在不破坏 css 和 co 且 URL 中不包含 index.php 的情况下实现对子文件夹 /store 的有效example.com/store/my-funny-test-product.html重写?example.com/store/city/my-funny-test-product.html

这是完整的 vhost 配置

    ## The whole setup stays close with the magento recommendations
    ## http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/configuring_nginx_for_magento

server {
    listen myip:80;
    server_name .example.com;

    root /var/www/mydomain/public_html;

    location / {
        index index.html index.php;
        try_files $uri $uri/ @handler;  
    }

    ## here comes my rewrite stuff to remove index.php from the subfolder ##
   location /store {
       rewrite ^/store /store/index.php;
   }

    ## followed by some deny rulesets not relevant here ##

    location @handler { ## Magento common front handler
        rewrite / /index.php;
    }

    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
        rewrite ^(.*.php)/ $1 last;
    }

    location ~ .php$ { ## Execute PHP scripts
        if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

        expires        off; ## Do not cache dynamic content
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  HTTPS $fastcgi_https;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }

答案1

现在 example.com/store/my-funny-test-product.html 可以工作,但是 CSS 图像等都损坏了!

使用尝试文件

目前,对 css 文件(或 中的任何实际文件/var/www/example.com/public_html/store)的请求不起作用,因为请求被无条件路由到/store/index.php。要使其正常工作,所需的最小更改是使用try_files

## here comes my rewrite stuff to remove index.php from the subfolder ##
location /store {
    # rewrite ^/store /store/index.php; NO
    try_files $uri /store/index.php;
}

这样,假如存在如下文件:

/var/www/example.com/public_html/store/css/style.css

然后以下 url 将返回其内容:

http://example.com/store/css/style.css

/store并且任何以 开头且不直接映射到文件的请求都将被传递给/store/index.php

答案2

然后将以下代码添加到这个新创建的 .htaccess 文件中

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

相关内容