1

I am trying to use .htaccess to make my url's "prettier".

Changing domain.com/page/val1

To domain.com/page.php?var1=val1

If possible, I would like it to work with 2 variables as well:

Changing domain.com/page/val1/val2

To domain.com/page.php?var1=val1&var2=val2

This is my current .htaccess file that works to remove the .php extension:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

I've found other topics with solutions similar to adding something like this:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1/$2?var1=$3&var2=$4 [L,QSA,NC]

I've tried many ways to modify it and it never seems to work.

Any help would be appreciated. Thanks.

anubhava
  • 761,203
  • 64
  • 569
  • 643
JettW
  • 15
  • 4

1 Answers1

3

Try this code:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^/]+)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?var1=$2 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?var1=$2&var2=$3 [L,QSA]
JettW
  • 15
  • 4
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks. It seems to work when I changed the placement of some variables. **But** for some reason, links to stylesheets and images aren't working in my page anymore. – JettW Jan 28 '14 at 05:20
  • I seem to have fixed the src issue by using the full url of the image/stylesheet. Not preferable, but it works. – JettW Jan 28 '14 at 05:57
  • For pretty URLs to work it is always better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with `http://` or a slash `/`. – anubhava Jan 28 '14 at 06:58