12

I've searched for this question but I only come across really specific answers that seem difficult to tailor to my specific needs.

Let's say the URL I'm attempting to rewrite is this:

http://www.example.org/test.php?whatever=something

I want to rewrite it so that it appears as this:

http://www.example.org/test/something

How can I do this?

UserIsCorrupt
  • 4,837
  • 15
  • 38
  • 41

1 Answers1

27

In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L]

And in order to redirect the query string URL to the nicer looking one:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+)
RewriteRule ^/?test\.php$ /test/%1? [L,R=301]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • If by using above code if someone having issue in trailing query string then they just need to add trailing question mark at end of the last rule, here is a updated code `RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+) RewriteRule ^/?test\.php$ /test/%1? [L,R=301]` – Raviraj Deora Aug 04 '14 at 14:59
  • i have use the same rules but its not working my url is http://example.com/directory1/directory2/login.php?id=8 and i want to make it like http://example.com/directory1/directory2/login/8 please tell me how to fix it – Deepak Goyal Jan 05 '16 at 04:56