Search code examples
.htaccessurlmod-rewritepermalinks

.htaccess - changing urls - RewriteRule not working


I'm fairly new to this, hopefully you can help me fix this problem.

I have a URL structure that gets an id from a database and shows the url like this:

www.website.com/post.php?P=18

I would like to present the URL's as:

www.website.com/post/18

In my .htaccess file, I've altered it like so:

RewriteEngine on
RewriteRule ^post/(\w+)$ post.php?P=$1

I've read through a few posts about this here on SO but I can't seem to figure it out.

I followed this:

The Rule:
RewriteRule ^user/(\w+)/?$ user.php?id=$1

Pattern to Match:
^              Beginning of Input
user/          The REQUEST_URI starts with the literal string "user/"
(\w+)          Capture any word characters, put in $1
/?             Optional trailing slash "/"
$              End of Input

Substitute with:
user.php?id=   Literal string to use.
$1             The first (capture) noted above.

Thank you!


Solution

  • I think it's important to share this kind of information for others that may be having the same issue so here goes.

    The Problem:

    [1] A link that looked like: www.example.com/news.php?P=1

    The link should look like www.example.com/news/1

    Then, the link would have to end up showing text instead of an ID. www.example.com/news/news-name

    The solution

    First, i had anchor tags that looked like this

    <a href="news.php?P='.$row['post_id'].'" class="btn btn-link"></a>
    

    It gave the first result in the URL [1]. To change it so it would present as a www.example.com/news/1, i had to do the following:

    Create a htaccess file and fill it like this:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [NC]
    ### THIS IS AN EXAMPLE FOR MULTIPLE EXPRESSIONS ###
    #RewriteRule ^news/([0-9]+)/([0-9a-zA-Z_-]+) news.php?P=$1&name=$2 [NC,L]
    
    RewriteRule ^news/([0-9]+) news.php?P=$1 [NC,L]
    

    And then, change the anchor tags to: <a href="news/'.$row['post_id'].'" class="btn btn-link"></a>

    [1] Would be done now.

    The challenge now was to use a slug instead of an ID. On the post creation page, I added the following PHP:

    <?php
    setlocale(LC_ALL, 'en_US.UTF8');
    function slugit($str, $replace=array(), $delimiter='-') {
        if ( !empty($replace) ) {
            $str = str_replace((array)$replace, ' ', $str);
        }
        $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
        $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
        $clean = strtolower(trim($clean, '-'));
        $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
        return $clean;
    }
    ?>
    

    Then, on the news insertion page, I added: $slug = slugit("$entry1");, which would pass $entry1 = $_POST['title']; as the title of the page, but slugified. On the database for the news, i created a column to accommodate $slug as a permalink name.

    Now to present the URL with the slug, I had to change the anchor tag to:

    <a href="news/'.$row['permalink'].'" class="btn btn-link"></a>
    

    And on the htaccess, change RewriteRule ^news/([0-9]+) news.php?P=$1 [NC,L] to RewriteRule ^news/([0-9a-zA-Z_-]+) news.php?P=$1 [NC,L]

    That's how I got it to work. I hope this will help people with similar problems fix theirs.