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

我有一个nginx vHost主机:

  • CMS在/
  • /store一个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 

现在我必须build立一个redirect,以便index.php可以从URL中删除。

 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] 

基于这个我build立了以下重写

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

现在example.com/store/my-funny-test-product.html作品,但CSS图像和类似的东西坏了! 我试图添加if (!-e $request_filename)来解决,但得到了一个nginx 40x错误。

我怎样才能实现工作重写example.com/store/my-funny-test-product.htmlexample.com/store/city/my-funny-test-product.html子文件夹/商店没有打破CSS&有没有index.php在URL中?

这是完整的虚拟主机configuration

  ## 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 } 

现在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

然后将下面的代码添加到这个新创build的.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} ^[AZ]{3,}\s(.*)/index\.php [NC] RewriteRule ^ %1 [R=301,L]