winnfield
winnfield

Reputation: 273

Module rewrite rules from PrestaShop generator

I generated .htaccess file by PrestaShop .htaccess file generator. There are rules that don't work properly.

This is generated .htaccess file:

    <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

For example this rewrite rule doesn't work.:

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

This works well:

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

This is rewrite log generated after I tried to enter pages: mysite.dev.com/contact-us and mysite.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

Upvotes: 2

Views: 3873

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

The reason why RewriteRule ^contact-us$ /contact-form.php [QSA,L] doesn't work:

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

It's matching a (.*) and getting passed through, without rewriting, and since /contact-us doesn't exist, it triggers the 404. This rule is causing the passthrough:

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

The reason why RewriteRule ^my-account$ /my-account.php [QSA,L] does work:

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

Well... it's not working. That rule is never getting applied. The exact same thing is happening as the request for /contact-us, except that you'll notice when you request /my-account, what actually ends up going through the rewrite engine (as a SUBREQUEST) is /my-account.php. The php extension got added onto the end before it even got to the rewrite engine, as a SUB REQUEST.

This makes me think you have Multiviews turned on somewhere, which is making all the request where all you need to do is append a .php extension to the end appear to work because it's immediately being passed-through by the RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] rule. But, of course, any request for something like contact-us, which require some bit of rewriting (to contact-form.php) is failing because something like Multiviews won't touch it.


I don't know why you have the HTTP authotization header, but it's making all your request get passed through without any rewriting. You can try removing that (at least for test purposes) and turning off multiviews at the top of the htaccess file:

Options -Multiviews

and see if your rules are working.

Upvotes: 1

Related Questions