.htaccessmod-rewritegetparameter

Creating mod_rewrite rule with one GET variable regardless of url-level


I'm trying to get this scenario to work:

URL: www.domain.com/sub/level/details/123/ Should be processed as: www.domain.com/sub/level/?product=123

"sub" and "level" can vary. It could also be /foo/doo/details/1/ or with even more sublevels: /foo/bar/foo/bar/foo/details/123/

Only the part: "/details/" followed by an integer/id isn't changing.

I tried and modified the .htaccess file from an older TYPO3 installation:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^de/(.*)$  http://test.domain.com/$1 [R=301,L]

RewriteRule ^typo3$ - [L]
RewriteRule ^typo3/.*$ - [L]
RewriteRule ^uploads/.*$ - [L]
RewriteRule ^fileadmin/.*$ - [L]
RewriteRule ^typo3conf/.*$ - [L]
RewriteRule ^./details/([0-9]+)/$ ?product=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule .* /index.php

I would be very grateful for your support!

Thanks


Solution

  • Your regex is incorrect as your matching a single character at start before /detail/.

    Replace this rule:

    RewriteRule ^./details/([0-9]+)/$ ?product=$1
    

    With this:

    RewriteRule ^(.+)/details/(\d+)/?$ $1?product=$2 [L,NC,QSA]