.htaccess - mod_rewrite trouble with /product/?id={xyz} to /product/{xyz} -
my attempt:
rewritecond %{query_string} ^id=(.*)$ [nc] rewriterule ^/product$ /product/%1 [nc,l,r=301] i want apply rule /product/ , /supplier/ directories only. both first level sub directories.
note: product/?id={xxx} product/index.php?id={xxx}. apache hides extensions , indexes. want point out.
my product/index.php handles parameter given , determines page should show:
index.php
if ( isset( $_get['id'] ) && !empty( $_get['id'] ) ) { //html individual page e.g. /product/?id=foo //e.g. <h1><?= $_get['id'] ?> page</h1> } else { //html product list e.g. /product/ (no parameters) }
try in 1 .htaccess file @ root directory:
options +followsymlinks -multiviews rewriteengine on rewritebase / rewritecond %{query_string} id=(.+) [nc] rewriterule ^(product|supplier)/?$ /$1/%1? [nc,l,r=301] in .htaccess files uri-path test in rule not have leading slash (^/product), therefore regex cannot have either. trailng ? deletes incoming query.
if rule-set placed @ apache main configuration files, leading slash should kept: ^/(product|supplier)/?$
update
to show wanted url still data original url.
request: /product/?id=parameter
options +followsymlinks -multiviews rewriteengine on rewritebase / rewritecond %{the_request} ^(get|head)\s/(product|supplier)/\?id=([^\s]+) [nc] # strip query , redirect permanently rewriterule ^(product|supplier) /$1/%3? [r=301,l,nc] rewritecond %{request_filename} !-f rewritecond %{query_string} ^$ # map internally original request rewriterule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [l,nc] another option use directly "pretty" url in request:
request /product/parameter /product/?id=parameter
options +followsymlinks -multiviews rewriteengine on rewritebase / rewritecond %{request_filename} !-f rewritecond %{query_string} ^$ rewriterule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [l,nc]
Comments
Post a Comment