I cannot get .htaccess rewrite rule to work with m

2019-07-19 02:30发布

问题:

Okay, I used an automated generator to generate a custom URL for my prestashop site in the .htaccess file, but it does not work. Any ideas?

the original URL is:

http://www.example.com/de/suche?controller=search&orderby=position&orderway=desc&search_query=mutter&submit_search=OK

the rewritten url should be:

http://www.example.com/search/position/desc/mutter/OK.html

and the rewrite rule I have in the .htaccess file is:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /de/suche?controller=$1&orderby=$2&orderway=$3&search_query=$4&submit_search=$5 [L]

回答1:

You should remove the [L] flag from your rule. It prevents any other rule to apply but you need to passe this new route to Prestashop dispatcher.

<IfModule mod_rewrite.c>
    RewriteEngine on

    #Domain: test.example.com
    RewriteRule . - [E=REWRITEBASE:/]
    RewriteRule ^api$ api/ [L]
    RewriteRule ^api/(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
    # Images
    RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
    # [...]

# You need to put your condition here without the [L] flag
# And before the Dispatcher
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /de/suche?controller=$1&orderby=$2&orderway=$3&search_query=$4&submit_search=$5
# Your URL is then rewritten to Prestashop standard format
# and will be sent to the following Dispatcher.

    # Dispatcher
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php [NC,L]
</IfModule>