.htaccess help

Discussion in 'PHP' started by medialab, Apr 15, 2014.

  1. #1
    Hey Everyone,

    I have a small blog script that uses 2 files, index.php and post.php. The slug of each article is being controlled through the .htaccess file:

    RewriteEngine On
    RewriteRule ^/(.*) /post.php?slug=$1

    The URL looks like this http://www.website.com/slug-goes-here

    However it doesn't work. I just get a 404 error.

    If I change the rewrite rule to:

    RewriteRule ^blog/(.*) /post.php?slug=$1

    http://www.website.com/blog/slug-goes-here works great! I just don't want the word blog in there. Any idea how to make this work?? I am so lost!

    Thanks!!
     
    medialab, Apr 15, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Just do ^(.*)$ post.php?slug=$1
     
    PoPSiCLe, Apr 15, 2014 IP
  3. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #3
    Thanks, but I have tried that and it does not work at all, the whole site just goes offline. It says to many redirects. Any other ideas?
     
    medialab, Apr 15, 2014 IP
  4. GuiltyCrown

    GuiltyCrown Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Maybe this?
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)$ http://blog.domain.com/post.php?slug=$1 [R=301,L]
    Code (markup):
    http://kb.mediatemple.net/questions/85/Using+.htaccess+rewrite+rules
     
    GuiltyCrown, Apr 16, 2014 IP
  5. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #5
    medialab, Apr 16, 2014 IP
  6. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #6
    For those looking, after what felt like 24 hours of trial and error I have found this and it works:

    RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?slug=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ post.php?slug=$1
    Code (markup):
    Thanks!
     
    medialab, Apr 16, 2014 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    This is why I consider parsing the URI to turn it into GETDATA to be ... well... a REALLY stupid way of doing things.

    I like to use a whitelist of files to serve normally instead, and route EVERY other request to a single index.php

    Of course if you've got sloppy multiple entry points (like post.php, login.php, etc, etc) that the user can call directly, that can be a bit of a fight to switch it around.
     
    deathshadow, Apr 17, 2014 IP