skip to Main Content

My file is category.php?id= and here is my code:

<a href="category.php?id=<?php echo $row['id'];?>"><?php echo $row['name'];?></a>

I want to change url to site.com/category/title-here/id. How can I rewrite it with .htaccess ?

Here is my .htaccess for detail.php page and it is working fine as site.com/detail.php?id=1 to site.com/title-here/1:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.name.com [NC]
RewriteRule ^(.*)$ http://name.com/$1 [L,R=301]

AddDefaultCharset UTF-8
RewriteRule ^/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2
RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1

Options -Indexes

2

Answers


  1. As for detail page you can add the same rule with a category prefix:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.name.com [NC]
    RewriteRule ^(.*)$ http://name.com/$1 [L,R=301]
    
    AddDefaultCharset UTF-8
    RewriteBase /
    RewriteRule ^(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
    RewriteRule ^category/([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ category.php?title=$1&$id=$2
    RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2
    RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1
    
    Options -Indexes
    
    Login or Signup to reply.
  2. Try This

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.name.com [NC]
    RewriteRule ^(.*)$ http://name.com/$1 [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^category/([^.]+)$ category.php [NC,L]
    RewriteRule ^/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
    RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2
    
    RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1
    Options -Indexes
    

    Now Your URL is Showing Like this http://name.com/category/your-title-here?id=1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search