URL Rewrite question

Discussion in 'Apache' started by ademmeda, Oct 8, 2012.

  1. #1
    Hi,

    I know how to rewrite URLs like the following:

    mysite.com/post/post-1/
    mysite.com/category/category-1/


    using

    RewriteRule ^post/([a-z0-9-]+)/$ post.php?post=$1
    RewriteRule ^category/([a-z0-9-]+)/$ category.php?category=$1
    Code (markup):
    What I want to achieve is to remove "post" and "category" from the URLs and have the following:

    mysite.com/post-1/
    mysite.com/category-1/


    Is this possible with .htaccess or do I need to have some sort of PHP solution?

    Thanks for any ideas.
     
    ademmeda, Oct 8, 2012 IP
  2. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    With plain .htaccess redirection/mod_rewrite , definitely NO. You would need some php solution for that trick, to which you can redirect all requests and then have your PHP script sort it out to either a category or a post. However, what might be a good in-between solution is to leave your categories the way they are, and have the posts with shorten url. For example:
    
    RewriteRule ^category/([a-z0-9-]+)/$ category.php?category=$1
    RewriteRule ^([a-z0-9-]+)/$ post.php?post=$1
    
    Code (markup):
    In that way your categories will remain in a form:
    mysite.com/category/category-1/
    but your posts would be like you wished:
    mysite.com/post-1/
    Just make sure to put the rules in your .htacess in order I wrote them, so the first one listed is your category rule, and then posts rule after that.

    Cheers.
     
    pr0t0n, Oct 8, 2012 IP
  3. wetbupa

    wetbupa Peon

    Messages:
    119
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Please use Mod Rewrite Generator from
    and it will help you to set up all types of .htaccess redirections and rewrites.
    [h=1][/h]
     
    wetbupa, Oct 8, 2012 IP
  4. ademmeda

    ademmeda Active Member

    Messages:
    354
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    70
    #4
    Thanks for the idea. Actually, I decided to use some folders as directories instead of dealing with such a PHP script that you mentioned. Maybe I try that route at another time.

    Sorry, it doesn't help at all.
     
    ademmeda, Oct 10, 2012 IP
  5. wetbupa

    wetbupa Peon

    Messages:
    119
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Then, better use Apache official site to set your rewrites yourself.
     
    wetbupa, Oct 10, 2012 IP