This question already has an answer here:
I am not familiar with url rewrite at all, so I have probably simple question for somebody that knows.
<a href="/category.php?id=<?php echo $row['id'];?>/<?php echo str_replace(' ', '-', strtolower($row['name']));?>"><?php echo $row['name'];?></a>
my url is http://birthdaycakenames.com/category.php?id=1/all-happy-birthday-cake
and i want to like this http://birthdaycakenames.com/category/1/all-happy-birthday-cake
here is my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.birthdaycakenames.com [NC]
RewriteRule ^(.*)$ http://birthdaycakenames.com/$1 [L,R=301]
AddDefaultCharset UTF-8
RewriteRule ^/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^category/$ category.php
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2
RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1
RewriteRule ^category/(.+)$ category.php?id=$1&id=$2
Options -Indexes
</div>
Try This
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.birthdaycakenames.com [NC]
RewriteRule ^(.*)$ http://birthdaycakenames.com/$1 [L,R=301]
AddDefaultCharset UTF-8
RewriteRule ^/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^category/$ category.php
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2
RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1
RewriteRule ^category/([^/]+)/(.*)$ category.php?id=$1&name=$2
Options -Indexes
This rule definition does not make much sense: you use two back references ($1
and &2
) but have only one capture bracket pair in your regular expression:
RewriteRule ^category/(.+)$ category.php?id=$1&id=$2
Have a try like that instead:
RewriteRule ^category/([^/]+)/(.*)$ category.php?id=$1&name=$2
Question here is if you need the name (the value the second back reference resolves to) at all. Since typically a script prefers to operate on an ID instead. If that argument is not required, then you can simply remove it from the target specification of the RewriteRule
:
RewriteRule ^category/([^/]+)/? category.php?id=$1
A general note: actually the URL you currently generate within php is invalid. A query string like id=1/all-happy-birthday-cake
is invalid, you'd have to either escape the slash (/
) in there or specify the name as a second, separate argument: http://birthdaycakenames.com/category.php?id=1&name=all-happy-birthday-cake
You probably also want to add a QSA
flag to that rule:
RewriteRule ^(.*)$ http://birthdaycakenames.com/$1 [L,R=301,QSA]
Apart from that I am not sure if that is really what you want: RewriteRule ^category/$ category.php
, since that means the script category.php
will get called without any arguments. That might be fine, for example if that script is able to work without to present a list of available categories or similar. But it is not exactly typical.
To change http://birthdaycakenames.com/category.php?id=1/all-happy-birthday-cake
to http://birthdaycakenames.com/category/1/all-happy-birthday-cake
you need the following 2 Rules :
RewriteEngine on
RewriteCond %{THE_REQUEST} /category\.php\?id=([^/]+)/([^\s&]+) [NC]
RewriteRule ^ /category/%1/%2? [L,R]
RewriteRule ^category/([^/]+)/([^/]+)/?$ /category.php?id=$1/$2 [NC,L]