【问题标题】:Magento. Nginx. Multiple store. How to add 'index.php' to urlsMagento。 Nginx。多家门店。如何将“index.php”添加到网址
【发布时间】:2015-06-22 20:37:24
【问题描述】:

我用 3 个商店下载了 magento,并尝试将其安装在本地主机上。

  • Ubuntu 14
  • Nginx
  • Mysql
  • php-fpm

    /app
    /downloader
    /includes
    ...
    /store1/index.php
    /store2/index.php
    

在我的store1/index.php 里面我放了$mageRunCode = 'my_store_code'

当我打开 url http://mysite.local.dev/store1/ 时,一切都可以正常打开,但所有链接都不包含 'index.php',否则所有 url 都不会打开。

返回 404:

http://mysite.local.dev/store1/some-cms-page.html

开得好:

http://mysite.local.dev/store1/index.php/some-cms-page.html

您能否告诉我如何重写网址以将“index.php”添加到我的网址中,或者请提供其他更清晰的解决方案。

提前致谢

server {

    listen 80;
    server_name local.dev *.local.dev;
    root /var/www/local.dev/www/$subdomain;
    set $subdomain "";
    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
    }
    if ($host ~* ^www.local.dev$) {
        set $subdomain "";
    }

    location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }

    ## These locations would be hidden by .htaccess normally
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }

    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
        auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
        autoindex            on;
    }

    location  /. { ## Disable .htaccess and other hidden files
        return 404;
    }

    location /api {
        rewrite ^/api/rest /api.php?type=rest last;
    }

    location @handler { ## Magento uses a 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 unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 600; 
        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
    }

}

根据Emi的解决方案。

如果我清楚地理解你 - 这是更新的配置:

server {

    listen 80;
    server_name local.dev *.local.dev;
    root /var/www/local.dev/www/$subdomain;
    set $subdomain "";
    set $magento_run_code "";
    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
        set $magento_run_code $1;
    }
    if ($host ~* ^www.local.dev$) {
        set $subdomain "";
        set $magento_run_code "";
    }

    location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }

    ## These locations would be hidden by .htaccess normally
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }

    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
        auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
        autoindex            on;
    }

    location  /. { ## Disable .htaccess and other hidden files
        return 404;
    }

    location /api {
        rewrite ^/api/rest /api.php?type=rest last;
    }

    location @handler { ## Magento uses a 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 unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 600; 
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE $magento_run_code; ## Store code is defined in administration > Configuration > Manage Stores
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }

}

在我的 core_store 表中,我有以下内容(名称已替换):

default - sitename
default_store1 - store1
default_store2 - store2

不安全的网址是:

http://sitename.local.dev/
http://sitename.local.dev/store1/
http://sitename.local.dev/store2/

当我尝试打开 sitename.local.dev 时,它会尝试使用 $mage_run_code = sitename 打开。那是因为我得到 404。我认为预期值应该是 default

store1 的 url 应该是什么?站点名称.local.dev/store1 ?

更新.1 我理解你的想法。

当我们有

    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
        set $magento_run_code $1;
    }

magento 以 $1 开始执行,它等于第三个域 lvl 中的值。在我的情况下 http://sitename.local.dev - 它是 sitename

我改成

    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
        set $magento_run_code default;
    }

现在它适用于第一家商店。

让我们为 store1 应用您的解决方案

根据这个解决方案,我目前的问题是:

目录:{magento_root}/store1/*

mage_run_code: default_store1

网址:http://store1.local.devhttp://sitename.local.dev/store1/ ??????

nginx 配置:据我所知,它将取决于 url。我应该使用哪个 url 和哪个配置?

【问题讨论】:

    标签: php magento nginx url-rewriting multistore


    【解决方案1】:

    如果您想在 3 个不同的商店中运行 Magento,为什么不使用默认设置? 默认含义,您在 nginx 配置中设置一些变量,具体取决于您的主机,在您的情况下,您可以使用类似 $subdomain: 的东西:

    set $magento_run_code ""; #default value
    if ($host ~* ^([a-z0-9-\.]+)\.local.dev$) {
        set $subdomain $1;
        set $magento_run_code $1;
    }
    if ($host ~* ^www.local.dev$) {
        set $subdomain "";
        set $magento_run_code "";
    }
    

    然后在你必须设置的位置下:

    fastcgi_param  MAGE_RUN_CODE $magento_run_code;
    

    这些值必须从您的管理员(系统 > 管理商店)中定义,否则 Magento 将无法加载该商店。

    也可以使用 index.php 在系统 > 配置 > 一般网络 > 搜索引擎优化中从管理员进行调整

    有关您加载的商店代码的更多信息,请查看 index.php 中的这些行:

    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
    
    /* Run store or run website */
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
    

    【讨论】:

    • 更新了我的第一篇文章
    • 好的,您是否检查了每个商店的不安全和安全的基本网址?这可以从 System > Configuration > GENERAL Web > Unsecured 更改。从左侧列中,您可以更改当前配置范围以匹配您要为其设置基本 url 的商店 - 您必须为每个商店执行此操作。
    • 我添加了我当前的 nginx 配置并编写了我的不安全 url。
    • 不,它返回 404 页面。最有可能是因为它试图用 code = 'sitename' 打开。但是我的网站上没有这样的代码。只有 default 或 default_store1。
    • 那么,在你的 nginx 配置集 set $magento_run_code "default_store1" 而不是 $1 正如我所说的,你可以 var_dump index.php 中的值来查看 Magento 试图从那里加载和调试的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2016-03-26
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多