1

I'm working with mod_rewrite under .htaccess, and I'm trying to redirect (R=301) an URL like this :

http://domain/index.php?folder=AB_CD 

to an URL like this

http://domain/AB/CD/

How can I write the rule please ?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115

1 Answers1

1

Try the following code in root/.htaccess :

RewriteEngine On


RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]

Explaination :

RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]

Checks to ensure that the url (index.php) has query strings with specific key and value, ( folder=foo_bar) acording to the regex pattern, if the url has valid query strings then the rule is processed

RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]

index.php?query_strings gets redirected to /query/strings, if the condition is met.

Empty question mark ? at the end of the Rewrite target is important as it discards the orignal query strings, without it /index.php?folder=foo_bar redirects to /foo/bar/?folder=foo_bar appending the old query strings.

(Hope, this helps!)

Amit Verma
  • 40,709
  • 21
  • 93
  • 115