JettW
JettW

Reputation: 15

Replace GET variables with slashes .htaccess

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.

Upvotes: 1

Views: 2149

Answers (1)

anubhava
anubhava

Reputation: 785246

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]

Upvotes: 3

Related Questions