rainerbrunotte
rainerbrunotte

Reputation: 907

Rewrite htaccess URL base for multiple GET variables with PHP

I am looking to create a PHP file that can handle multiple GET variables and output data accordingly, however I would like to remove the base "category" URL element in order to shorten it, with htaccess, like so:

http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food

Currently I use different files for each type, but it is a pain:

RewriteRule ^news/([^/]+)/?$ tpl-news.php?slug=$1 [L,QSA,NC]
RewriteRule ^videos/([^/]+)/?$ tpl-videos.php?slug=$1 [L,QSA,NC]
RewriteRule ^posts/([^/]+)/?$ tpl-posts.php?slug=$1 [L,QSA,NC]
etc ...

Alternatively, I could use the following:

RewriteRule ^category/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA,NC]

I do not want to have a URL like:

http://website.com/category/news/politics
http://website.com/category/videos/funny
http://website.com/category/posts/food

But would like to remove the "category" from the URL like so:

http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food

However other pages / URLs, for example like the ones below, should not be affected by this rule:

http://website.com/account/user
http://website.com/newspost/abcdef // a single post page
http://website.com/videopost/abcdef // a single video page

These "types" (news, videos, posts, ...) are limited in number, i.e. I have no objection predefining those, so that they do not conflict with other URLs of the page (such as image folders, admin sections, user profile sub pages, etc)

Upvotes: 1

Views: 522

Answers (1)

anubhava
anubhava

Reputation: 784998

You can use this generic rule:

RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA]

Or if you want to use existing .php files e.g. tpl-news.php, tpl-post.php etc then use:

RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-$1.php?slug=$2 [L,QSA]

Upvotes: 1

Related Questions