krzysztof krzysztof - 1 year ago 104
Apache Configuration Question

htaccess rewrite part of url and leave rest as it is

I need to rewrite part of my url as a parameter and part as a path. Is it possible and if so can anyone please help me

To be more specific I have an url like this:


and what I need is


To make things more interesting depending on user query path after
can by shorter or longer

If I had constant number of parameters I would write something like that

RewriteRule ^products/([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)/?$ products.php/$1/$2 [L]

but as a number of parameters changes I have no idea what to do

That's my whole htaccess so far

Options -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} ^mysite\.pl
RewriteRule ^(.*)$$1 [R=permanent,L]
RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
AddDefaultCharset UTF-8
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 month"
Header append Cache-Control "public"

Answer Source

Method #1 just changes products and keeps everything else the same

RewriteRule ^products/$ products.php [NC,L,B,QSA]

Method #2

RewriteRule ^products/producer:(.*)/category:(.*)/color:(.*)$ products.php?producer=$1&category=$2&color=$3 [NC,L]

And here's probably what your htaccess file should look like.

Options -MultiViews
RewriteEngine On

# Prevents directory browsing
Options -Indexes

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript

# your 404 page
ErrorDocument 404 /path/to/your/404file.php

RewriteRule ^products/$ products.php [NC,L,B,QSA]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download