invidious
invidious

Reputation: 241

htaccess rewrite using regex not working with a dash

For my rewrite I have:

RewriteRule ^game/([a-z0-9-]+)/$ game.php?game=$1 [NC,L]

The URL being game/counter-strike/ is returning a copy of index.php (meaning it's saying game.php?game=$1 doesn't exist), but if I echo $_GET['game'] there then it echos counter-strike.

It works if the game name is one word, like mmorpg, just not with the dash. Is there an obvious problem here?

Upvotes: 1

Views: 578

Answers (1)

anubhava
anubhava

Reputation: 785128

Disable MultiViews option by placing this line at top of your .htaccess and have your rule like this

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^game/([a-z0-9-]+)/$ game.php?game=$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.
  • QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Upvotes: 3

Related Questions