Emilie_
Emilie_

Reputation: 11

.htaccess rewrite query string not working

I have search and tried all possible solutions from Stackoverflow but nothing seems to work for my case.

I'm trying to rewrite this URL:
http://www.example.com/test/name.php?name=somename

to appear as
http://www.example.com/test/somename

This is what I have currently in my htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/?$ /test/name.php/?name=$1 [NC,L,QSA]

Would appreciate if anyone can lend some help here as I have been stuck in this for more than a day. Thanks!

Upvotes: 1

Views: 1142

Answers (2)

Andrew Uldin
Andrew Uldin

Reputation: 1

Try to use RewriteBase!

RewriteEngine On
RewriteBase /test/

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . name.php?name=$1

Here "." is any request.

Upvotes: 0

Marc B
Marc B

Reputation: 360872

RewriteRule ^test/?$ /test/name.php/?name=$1 [NC,L,QSA]
                  ^---- "0 or 1 of the previous"

Because you've got ^$ anchors in there, you're basically saying

/test/
/test

Are the only two URIs which could match. you probably want

RewriteRule ^test/(.*)$ /test/name.php?name=$1

instead. "A url which starts with test/, and uses everything after test/ for the name parameter".

Upvotes: 1

Related Questions