Stichy Stichy - 4 months ago 13
PHP Question

mod-rewrite 2 parameters and include paths

I have 2 parameters cat and page_id

Today my URL:s are like: http://foo.com/cat=3&page_id=1

Can this be translated to http://foo.com/category/page_id ?

I have tried this in .htaccess:

RewriteEngine on
RewriteRule ^([\S-]+)/([\S-]+)/?$ ?cat=$1&page=$2 [L]


When I surf in to http://foo.com/category/page_id my CSS-link (css/style.css) counts as category and page_id and will therefor not load.

Any ideas?

Answer

You have to add exceptions like file or directory with this code before RewriteRule:

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

This will exclude all the situations when existing files or directories are requested.

So your final .htaccess file should look like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\S-]+)/([\S-]+)/?$   ?cat=$1&page=$2 [L]
Comments