Kaziko Kaziko -4 years ago 85
PHP Question

Mod Rewrite - ignore catalog before string

I have simple rewriting for blog articles using this directive

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?aid=$1 [QSA,L]


It works as intended when using urls like

http://example.com/some-blog-title


Lately I added special category for some post and need it urls in form

http://example.com/categoryname/some-blog-title


So what I need is to use both types of url on site

http://example.com/some-blog-title


and

http://example.com/categoryname/some-blog-title


and both of them using viewpost.php?aid=$1 script.
Categoryname is just a word (product name). It wont change so it doesnt need to be dynamic. I want to use it simply for seo purpose.
So i need this directive to accept both types of url - with categoryname or without it.

Answer Source

Just change your rule to this to make category name optional:

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(?:[\w-]+/)?([\w-]+)/?$ viewpost.php?aid=$1 [QSA,L]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download