1

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)

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @anubhava, it does work for the category pages, however now some other URLs are messed up with that rule. I added some more specifications above, see edits towards the bottom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.