1

Sorry if this is really simple! I'm not experienced with htaccess rewrites, so I'm just feeling my way around.

I have this URL

  /category.php?cat=projects/retail/

But I want rewrite it as a clean URL, so it looks like this:

  /category/projects/retail/

I've written this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^category.php/([a-zA-Z0-9-/]+)$ /category?s=$1 [L]
</IfModule>

I found a good article on media temple Using .htaccess rewrite rules. This explains a bit about the structure of rewrite rule - but I'm still a bit stuck

I know ^ starts the URL to match and $ ends it. After the $ the second string defines the new URL.

So this bit after the RewriteRule

^category.php/([a-zA-Z0-9-/]+)$ 

Defines the original URL, I think the content within the ([a-zA-Z0-9-/]+) means match uppercase, lowercase and hyphens.

I also know [L] tells the server to stop

What I don't know is how to rewrite the URL - yet!

Can anyone help?

StephenMeehan
  • 1,083
  • 2
  • 15
  • 26

1 Answers1

2

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+category\.php\?cat=([^\s&]+) [NC]
RewriteRule ^ /category/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^category/(.+?)/?$ category.php?cat=$1 [L,QSA,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643