【问题标题】:Module rewrite rules from PrestaShop generator来自 PrestaShop 生成器的模块重写规则
【发布时间】:2012-08-27 12:04:31
【问题描述】:

我通过 PrestaShop .htaccess 文件生成器生成了 .htaccess 文件。有些规则不能正常工作。

这是生成的.htaccess文件:

    <IfModule mod_rewrite.c>
    # URL rewriting module activation
    RewriteEngine on

    # URL rewriting rules
    RewriteRule ^api/?(.*)$ /webservice/dispatcher.php?url=$1 [QSA,L]
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
    RewriteRule ^([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1-$2$3.jpg [L]
    RewriteRule ^([0-9]+)\-([0-9]+)/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1-$2.jpg [L]
    RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$1$2.jpg [L]
    RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$2/$1$2$3.jpg [L]
    RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg [L]
    RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg [L]
    RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg [L]
    RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg [L]
    RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg [L]
    RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg [L]
    RewriteRule ^c/([0-9]+)(\-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*\.jpg$ /img/c/$1$2.jpg [L]
    RewriteRule ^c/([a-zA-Z-]+)/[a-zA-Z0-9-]+\.jpg$ /img/c/$1.jpg [L]
    RewriteRule ^([0-9]+)(\-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*\.jpg$ /img/c/$1$2.jpg [L]
    RewriteRule ^([0-9]+)\-[a-zA-Z0-9-]*\.html /product.php?id_product=$1 [QSA,L]
    RewriteRule ^[a-zA-Z0-9-]*/([0-9]+)\-[a-zA-Z0-9-]*\.html /product.php?id_product=$1 [QSA,L]
    RewriteRule ^([0-9]+)\-[a-zA-Z0-9-]*(/[a-zA-Z0-9-]*)+ /category.php?id_category=$1&noredirect=1 [QSA,L]
    RewriteRule ^([0-9]+)\-[a-zA-Z0-9-]* /category.php?id_category=$1 [QSA,L]
    RewriteRule ^([0-9]+)__([a-zA-Z0-9-]*) /supplier.php?id_supplier=$1 [QSA,L]
    RewriteRule ^([0-9]+)_([a-zA-Z0-9-]*) /manufacturer.php?id_manufacturer=$1 [QSA,L]
    RewriteRule ^content/([0-9]+)\-([a-zA-Z0-9-]*) /cms.php?id_cms=$1 [QSA,L]
    RewriteRule ^content/category/([0-9]+)\-([a-zA-Z0-9-]*) /cms.php?id_cms_category=$1 [QSA,L]
    RewriteRule ^page-not-found$ /404.php [QSA,L]
    RewriteRule ^address$ /address.php [QSA,L]
    RewriteRule ^addresses$ /addresses.php [QSA,L]
    RewriteRule ^authentication$ /authentication.php [QSA,L]
    RewriteRule ^best-sales$ /best-sales.php [QSA,L]
    RewriteRule ^cart$ /cart.php [QSA,L]
    RewriteRule ^contact-us$ /contact-form.php [QSA,L]
    RewriteRule ^discount$ /discount.php [QSA,L]
    RewriteRule ^guest-tracking$ /guest-tracking.php [QSA,L]
    RewriteRule ^order-history$ /history.php [QSA,L]
    RewriteRule ^identity$ /identity.php [QSA,L]
    RewriteRule ^manufacturers$ /manufacturer.php [QSA,L]
    RewriteRule ^my-account$ /my-account.php [QSA,L]
    RewriteRule ^new-products$ /new-products.php [QSA,L]
    RewriteRule ^order$ /order.php [QSA,L]
    RewriteRule ^order-follow$ /order-follow.php [QSA,L]
    RewriteRule ^quick-order$ /order-opc.php [QSA,L]
    RewriteRule ^order-slip$ /order-slip.php [QSA,L]
    RewriteRule ^password-recovery$ /password.php [QSA,L]
    RewriteRule ^prices-drop$ /prices-drop.php [QSA,L]
    RewriteRule ^search$ /search.php [QSA,L]
    RewriteRule ^sitemap$ /sitemap.php [QSA,L]
    RewriteRule ^stores$ /stores.php [QSA,L]
    RewriteRule ^supplier$ /supplier.php [QSA,L]
    </IfModule>

    # Catch 404 errors
    ErrorDocument 404 /404.php

例如这个重写规则不起作用。:

    RewriteRule ^contact-us$ /contact-form.php [QSA,L]

这很好用:

    RewriteRule ^my-account$ /my-account.php [QSA,L]

这是我尝试进入页面后生成的重写日志:mysite.dev.com/contact-usmysite.dev.com/my-account

    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c7e80a0/initial] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/contact-us -> contact-us
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c7e80a0/initial] (3) [perdir /srv/www/mysite/] applying pattern '^api/?(.*)$' to uri 'contact-us'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c7e80a0/initial] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/contact-us -> contact-us
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c7e80a0/initial] (3) [perdir /srv/www/mysite/] applying pattern '.*' to uri 'contact-us'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c7e80a0/initial] (1) [perdir /srv/www/mysite/] pass through /srv/www/mysite/contact-us
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e82b0/initial/redir#1] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/404.php -> 404.php
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e82b0/initial/redir#1] (3) [perdir /srv/www/mysite/] applying pattern '^api/?(.*)$' to uri '404.php'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e82b0/initial/redir#1] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/404.php -> 404.php
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e82b0/initial/redir#1] (3) [perdir /srv/www/mysite/] applying pattern '.*' to uri '404.php'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e82b0/initial/redir#1] (1) [perdir /srv/www/mysite/] pass through /srv/www/mysite/404.php
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8db0a0/initial] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/page-not-found -> page-not-found
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8db0a0/initial] (3) [perdir /srv/www/mysite/] applying pattern '^api/?(.*)$' to uri 'page-not-found'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8db0a0/initial] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/page-not-found -> page-not-found
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8db0a0/initial] (3) [perdir /srv/www/mysite/] applying pattern '.*' to uri 'page-not-found'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8db0a0/initial] (1) [perdir /srv/www/mysite/] pass through /srv/www/mysite/page-not-found
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e62b8/initial/redir#1] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/404.php -> 404.php
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e62b8/initial/redir#1] (3) [perdir /srv/www/mysite/] applying pattern '^api/?(.*)$' to uri '404.php'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e62b8/initial/redir#1] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/404.php -> 404.php
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e62b8/initial/redir#1] (3) [perdir /srv/www/mysite/] applying pattern '.*' to uri '404.php'
    127.0.0.1 - - [27/Aug/2012:13:47:50 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8e62b8/initial/redir#1] (1) [perdir /srv/www/mysite/] pass through /srv/www/mysite/404.php
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c86b0a0/subreq] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/my-account.php -> my-account.php
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c86b0a0/subreq] (3) [perdir /srv/www/mysite/] applying pattern '^api/?(.*)$' to uri 'my-account.php'
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c86b0a0/subreq] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/my-account.php -> my-account.php
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c86b0a0/subreq] (3) [perdir /srv/www/mysite/] applying pattern '.*' to uri 'my-account.php'
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c86b0a0/subreq] (1) [perdir /srv/www/mysite/] pass through /srv/www/mysite/my-account.php
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8dd0a0/initial] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/my-account.php -> my-account.php
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8dd0a0/initial] (3) [perdir /srv/www/mysite/] applying pattern '^api/?(.*)$' to uri 'my-account.php'
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8dd0a0/initial] (3) [perdir /srv/www/mysite/] strip per-dir prefix: /srv/www/mysite/my-account.php -> my-account.php
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8dd0a0/initial] (3) [perdir /srv/www/mysite/] applying pattern '.*' to uri 'my-account.php'
    127.0.0.1 - - [27/Aug/2012:13:47:55 +0200] [mysite.dev.com/sid#7f77a22d6b80][rid#7f779c8dd0a0/initial] (1) [perdir /srv/www/mysite/] pass through /srv/www/mysite/my-account.php

【问题讨论】:

    标签: php apache mod-rewrite prestashop


    【解决方案1】:

    RewriteRule ^contact-us$ /contact-form.php [QSA,L]不起作用的原因:

    strip per-dir prefix: /srv/www/mysite/contact-us -> contact-us
    applying pattern '.*' to uri 'contact-us'
    pass through /srv/www/mysite/contact-us
    

    它匹配 (.*) 并通过,无需重写,并且由于 /contact-us 不存在,它触发 404。此规则导致通过:

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
    

    RewriteRule ^my-account$ /my-account.php [QSA,L] 起作用的原因:

    strip per-dir prefix: /srv/www/mysite/my-account.php -> my-account.php
    applying pattern '^api/?(.*)$' to uri 'my-account.php'
    strip per-dir prefix: /srv/www/mysite/my-account.php -> my-account.php
    applying pattern '.*' to uri 'my-account.php'
    pass through /srv/www/mysite/my-account.php
    

    嗯...它不工作。该规则永远不会得到应用。与/contact-us 的请求完全相同,除了您会注意到当您请求/my-account 时,实际上最终通过重写引擎(作为SUBREQUEST)是@ 987654330@。 php 扩展被添加到末尾甚至在它到达重写引擎之前,作为一个 SUB REQUEST。

    这让我觉得你在某处打开了Multiviews,这使得你需要做的所有请求就是在末尾附加一个.php 扩展似乎可以工作,因为它是立即被RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] 规则传递。但是,当然,任何像 contact-us 这样需要重写(到 contact-form.php)的请求都会失败,因为像 Multiviews 这样的东西不会触及它。


    我不知道为什么您有 HTTP 授权标头,但它使您的所有请求都通过而无需任何重写。您可以尝试删除它(至少出于测试目的)并关闭 htaccess 文件顶部的多视图:

    Options -Multiviews
    

    看看你的规则是否有效。

    【讨论】:

    • 谢谢,@jon-lin。它在我删除 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] 规则后起作用(它不是 Multiviews 选项)。你能告诉我这条规则是否必要吗?
    • @winnfield 不知道这是否重要。它将环境变量HTTP_AUTHORIZATION 设置为授权标头(如果有的话)。我不知道您是否在某处使用该变量。该规则的问题是L 正在使它停止。您可以尝试仅删除 L 标志。
    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 2014-10-04
    • 2014-02-02
    • 2010-10-29
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2016-04-26
    相关资源
    最近更新 更多