views:

43

answers:

2

Using htaccess how to turn this type of URL:

http://www.mysite.com/page.php?slug=hello-world

into this type:

http://www.mysite.com/page/hello-world

And not just this one url but all urls in the 1st format to the 2nd format.

+3  A: 

If the second one is the URL you want people to see, use this:

RewriteRule ^page/(.*)$ /page.php?slug=$1

If it's the other way around:

RewriteRule ^page\.php?slug=(.*)$ /page/$1

EDIT: Also, make sure you have the following in your .htaccess before ANY RewriteRules:

RewriteEngine On
Lauri Lehtinen
@Lauri Lehtinen this isn't working for me, I put the 1st block of code you posted in a .htaccess file which I placed at the root of my www folder. Then I tried to access the page by typing http://www.mysite.com/page/hello-world and it didn't work.
Jim
@Jim I edited my post
Lauri Lehtinen
Yes I added the rewrite engine on declaration, still it returns a 404 page not found error :(
Jim
I just tried this and it worked fine. Are any redirects working for you? mod_rewrite enabled, .htaccess not ignored?
Lauri Lehtinen
+1  A: 

You may want to refer to here and this

Here is my thought to your solution:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^slug=[^/]+$ [NC] 
RewriteRule ^page\.php$ http://www.mysite.com/page/$1? [R=301,L] 
Carson
What this code does is redirect me to mysite.com/product when I type mysite.com/product.php?slug=hello-world which of course is a not found. And if I type mysite.com/page/hello-world it too results in a 404.
Jim
could you specify your question more? the word 'turn' is not clear, do you mean frontend redirect / server redirect? and it is better to state what parameters in the url you want to change...
Carson