I need to rewrite URL's with and without parameters in the URL, I configured a RewriteMap
via dbm
kronen-und-gehaeusetuben/wasserdicht-typ-pbbz.html => Produkte/Werke-Uhrenersatzteile/Schraubkronen-PBBZ-wasserdicht/
kronen-und-gehaeusetuben/wasserdicht-typ-pbbz.html?___store=default => Produkte/Werke-Uhrenersatzteile/Schraubkronen-PBBZ-wasserdicht/
kronen-und-gehaeusetuben/wasserdicht-typ-pbbz.html?___store=en => en/Products/Watch-replacement-parts/Watch-crowns/Screw-on-crowns/Screw-on-crowns-PBBZ-waterproof/
kronen-und-gehaeusetuben/wasserdicht-typ-pbbz.html?___store=fr => fr/Produits/Couronnes/Couronnes-vissees/Couronnes-vissees-PBBZ-etanches/
kronen-und-gehaeusetuben/wasserdicht-typ-pbbz.html?___store=es => es/Productos/Movimientos-Piezas/Piezas-de-repuesto-para-relojes/Coronas/Coronas-a-rosca/Coronas-a-rosca-PBBZ-hermeticas/
<VirtualHost *:80>
DocumentRoot "/var/www/html"
#ServerName www.example.com
RewriteMap rewrite_DB "dbm:conf/rewrite.dbm"
<Directory "/var/www/html">
Options FollowSymLinks Includes ExecCGI
AllowOverride All
DirectoryIndex index.html
RewriteEngine On
LogLevel alert rewrite:trace4
RewriteBase /
RewriteRule "^(.*html)" "${rewrite_DB:$1|/}" [L,NC,R=301]
</Directory>
</VirtualHost>
It works but how should I handle the parameters like ?___store=default
?
All HTML files without parameters work but not those with ?___store=default
or similar.
CodePudding user response:
RewriteRule "^(.*html)" "${rewrite_DB:$1|/}" [L,NC,R=301]
The RewriteRule
pattern only captures the URL-path, which naturally excludes the query string. If you need to pass the entire URL (including the query string) to your rewrite map then consider capturing the value from THE_REQUEST
instead, which contains the entire first line of the request, including the query string.
For example, THE_REQUEST
server variable contains a string of the form:
GET /kronen-und-gehaeusetuben/wasserdicht-typ-pbbz.html?___store=es HTTP/1.1
So, you would write the rule like the following instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(. )\sHTTP
RewriteRule \.html$ ${rewrite_DB:%1|/} [L,NC,R=301]
The RewriteRule
pattern simply ensures that only .html
requests are checked.
You should probably remove the NC
flag - by the look of your rewrite map, you are only matching against html
(lowercase) anyway?
The %1
(as opposed to $1
) backreference contains the captured group from the preceding CondPattern, which is the URL-path and optional query string, less the slash prefix on the URL-path (which is also omitted from your rule).
Aside:
AllowOverride All
You are allowing .htaccess
overrides. If you have mod_rewrite directives in a .htaccess
file then they will (by default) completely override the mod_rewrite directives in the <Directory>
container in the vHost, so the rewrite map would never be processed.
CodePudding user response:
Just tested it, it seems to work but
werkzeuge-verbrauchsartikel/schraubendreher/hormec-schraubendreher-zubehoer.html?___store=default Produkte/Werkzeuge/Schraubendreher/Hormec-Schraubendreher-Zubehoer/
http://1.2.3.4/werkzeuge-verbrauchsartikel/schraubendreher/hormec-schraubendreher-zubehoer.htmlenter code here?___store=default
it get correctly redirected to http://1.2.3.4/Produkte/Werkzeuge/Schraubendreher/Hormec-Schraubendreher-Zubehoer/?___store=default
but the ?___store=default
should be removed (or not forwarded)
same for werkzeuge-verbrauchsartikel/schraubendreher/lecureux-elektrische-schraubendreher-zubehoer/lecureux-ersatzklingen.html?___store=en en/Products/Tools/Screwdrivers/Lecureux-screwdrivers-and-accessories/Lecureux-spare-blades/
http://1.2.3.4/werkzeuge-verbrauchsartikel/schraubendreher/lecureux-elektrische-schraubendreher-zubehoer/lecureux-ersatzklingen.html?___store=en
'http://1.2.3.4//en/Products/Tools/Screwdrivers/Lecureux-screwdrivers-and-accessories/Lecureux-spare-blades/?___store=en`
but the ?___store=en
should be removed (or not forwarded)
it work perfect for
werkzeuge-verbrauchsartikel/schraubendreher/lecureux-elektrische-schraubendreher-zubehoer/lecureux-elektrische-schraubendreher.html Produkte/Werkzeuge/Schraubendreher/Lecureux-Schraubendreher-Zubehoer/Lecureux-elektrische-Schraubendreher/
but in the requested URL it no ?___store=XX
thx Matthias