krzysztof
krzysztof

Reputation: 53

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:

www.mysite/products/producer:some+producer/category:some+category/color:red,blue,yellow

and what I need is

www.mysite/products.php/producer:some+producer/category:some+category/color:red,blue,yellow

To make things more interesting depending on user query path after www.mysite/products/ 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 ^(.*)$ http://www.mysite.pl/$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
## EXPIRES CACHING ##
<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"
</IfModule>
## EXPIRES CACHING ##

Upvotes: 2

Views: 1681

Answers (2)

hjpotter92
hjpotter92

Reputation: 80629

You can just use a .+ check:

RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]

Putting the floating directives in their respective module checks:

Options -MultiViews
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^mysite\.pl
    RewriteRule ^(.*)$ http://www.mysite.pl/$1 [R=permanent,L]
    RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]
</IfModule>
AddDefaultCharset UTF-8

<IfModule mod_filters.c>
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
    </IfModule>
</IfModule>
## EXPIRES CACHING ##
<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"
</IfModule>
## EXPIRES CACHING ##

Upvotes: 1

Trevor
Trevor

Reputation: 2457

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

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

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

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

Upvotes: 2

Related Questions