cameronjonesweb
cameronjonesweb

Reputation: 2506

.htaccess rewrite to work with and without $_GET variable

I have a player.php page, and if no playerid GET variable is defined then it will get the current user's as defined in a global variable. This is working. It's just when I try to implement rewrites I'm having problems.

First I had this:

RewriteEngine On

RewriteRule ^player/([^/]+) player.php?playerid=$1 [NC,L]

^ Worked fine if the playerid variable was set, but if not I just got 404

Next up I tried to fix that

RewriteEngine On

RewriteRule ^player player.php [NC]
RewriteRule ^player/([^/]+) player.php?playerid=$1 [NC,L]

^ So instead now they all just load up the current user's profile regardless of the playerid

So I changed approach completely

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteRule ^player/([^/]+) player.php?playerid=$1 [NC,L]

^ This worked for not setting a playerid, but if one it set it appends .php on the end. So it thinks that player/1 is player/1.php. Which of course breaks the queries I'm attempting to run to get the player info.

Upvotes: 1

Views: 98

Answers (1)

anubhava
anubhava

Reputation: 785186

Your first attempt is right but you need to disable MultiViews option.

Options -MultiViews
RewriteEngine On

RewriteRule ^player/([^/]+)/?$ player.php?playerid=$1 [NC,L,QSA]

Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Upvotes: 1

Related Questions