Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess rewrite query string as path

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?

like image 714
UserIsCorrupt Avatar asked Oct 21 '12 23:10

UserIsCorrupt


1 Answers

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]
like image 104
Jon Lin Avatar answered Nov 18 '22 06:11

Jon Lin