Bytes
Bytes

Reputation: 721

Rewrite URL to remove query string

I have a URL that needs to be reformatted. example.com/?article_category=lifestyle I want this to be formatted to: example.com/lifestyle. However, when I try to reformat it with my .htaccess file it doesn't work. My .htaccess file is located in the same directory as my file. The file name is index.php.

Here's the rewrite rule I proposed:

RewriteRule ^([\w-]+)/?$ index.php?article_category=$1 [QSA,L]

I don't know why this isn't working. What am I doing wrong?

Upvotes: 0

Views: 64

Answers (1)

Ben
Ben

Reputation: 5129

Useful Docs: RewriteCond Directive & RewriteRule Directive

RewriteCond %{QUERY_STRING} article_category=([a-z]+)
RewriteRule ^/?$ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+) index.php?article_category=$1 [QSA,L]

Line 1: checking if query string containing article_category=([a-z]+), article_category=lifestyle matched

Line 2: if the URL is example.com or example.com/, redirect permanently to /lifestyle, %1 is a RewriteCond backreference, grouped parts (in parentheses ([a-z]+)) of the pattern

Line 3 / 4: if the URL is not regular files and directories

Line 5: if the URL is starting with alphabetic, rewrite the URL to index.php?article_category=$1

Upvotes: 1

Related Questions