Reputation: 905
I'm trying to make an SEO friendly URL using an .htaccess file.
The url I am trying to change looks like this:
https://www.example.com/category/?title=a+really+really+long+title
and what I would like to have is:
https://www.example.com/category/a+really+really+long+title/
I have tried:
RewriteEngine On
RewriteRule ^/category/?title=(.+)$ ^/category/$1/ [NC]
but it does not give me the desired results. Any help would be most appreciated!
Upvotes: 0
Views: 91
Reputation: 17805
RewriteEngine On
RewriteCond %{QUERY_STRING} ^title\=(.+)$ [NC]
RewriteRule ^/?category/?$ /category/%1 [NC,R,L,QSD]
We first check to see if there is a query string present using RewriteCond
. If it matches ^title\=(.+)$
, meaning, if there is a query string with key title
, then capture the value of it.
Using RewriteRule, we match if request URI has a category
. If yes, then rewrite it as /category/%1
where %1
is the value of the title
key matched in rewrite condition for a query string. The %1
is a backreference(scroll down a bit) to the captured group number in the regex(of the query string).
Note that if you want to do it for any key-value pair in the query string, then change title
to \w+
or [a-zA-Z0-9]+
to be more precise.
Upvotes: 1