1

For example:

http://domain.com/comments?p=73819

becomes

http://domain.com/comments/73819.

How can I do this in my .htaccess file?

frosty
  • 2,779
  • 6
  • 34
  • 63

1 Answers1

2

Try:

Options -Multiviews
RewriteEngine On

RewriteCond %{THE_REQUEST} \ /comments\?p=([0-9]+)
RewriteRule ^ /comments/%1? [L,R]

RewriteRule ^comments/([0-9]+)$ /comments?p=$1 [L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • That works! Just a question, this won't interfere with for example `$_GET['p']` requests will it? – frosty Jul 29 '15 at 06:57
  • @frosty the first rule redirects requests with the query string to the one without, the second rule internally rewrites the request *back* to using the query string, so the php scripts will be able to fetch the `p` GET variable again – Jon Lin Jul 29 '15 at 06:59
  • Ah, so that way links will actually be able to fetch information since they'd lack the query string. Thanks. – frosty Jul 29 '15 at 07:05
  • it seems to be treating it like a folder because the CSS doesn't load unless I use `../`, and in addition when I attempt to echo `$_GET['p']` nothing is echoed. When I delete the rule it works fine – frosty Jul 29 '15 at 11:07
  • @frosty CSS is probably not working because you've changed the relative URI base by adding another `/` in the URL. If your css is linked using relative URLs, you need to make them all absolute – Jon Lin Jul 29 '15 at 13:48