user3302325
user3302325

Reputation: 55

Better way of implementing RewriteRule?

Is there a better way of doing this?

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^featured/([^/]+)$ /pages/featured.php?game=$1 [L]
RewriteRule ^featured/([^/]+)/$ /pages/featured.php?game=$1 [L]
RewriteRule ^featured/ /pages/featured.php [L]
RewriteRule ^featured /pages/featured.php [L]

Example: I want user on this page

https ://www.example.com/featured/tetris/

to be rewritten to this page

https ://www.example.com/pages/featured.php?game=tetris

and the trailing slash doesn't matter.

Upvotes: 2

Views: 36

Answers (1)

mario
mario

Reputation: 145482

You can compact your rules into:

#         optional /gamename   slash
                      ↓         ↓
RewriteRule ^featured(?:/([^/]*)/?)?$ pages/featured.php?game=$1 [L]

That way you'll receive an empty $_GET["game"] if the request was just for /featured or /featured/. It will be populated for /featured/whatever/ however.

Upvotes: 2

Related Questions