Home > other >  RewriteRule for specific Query String
RewriteRule for specific Query String

Time:03-12

I’d like to use .htaccess to rewrite a URL like this

https://example.com/pages/games/demons-souls/?page=mods

to look like this

https://example.com/games/demons-souls/mods

I managed to hide the pages folder in the URL with this code:

Options  MultiViews
RewriteEngine On

# Rewrite "/games/<anything>" to "/pages/games/<anything>"
RewriteCond %{REQUEST_URI} !^/pages/games
RewriteRule ^games($|/.*) pages/$0

But I need a proper RewriteRule for the Query String. I tried this but it didn’t work ...

RewriteRule ^([^/]*)/([^/]*)$ $1/index.php?page=$2 [L,QSA]

I’d really appreciate some help.

CodePudding user response:

You can have it like this:

Options  MultiViews
RewriteEngine On

# Rewrite "/games/<foo>/<bar>" to "/pages/games/<foo>/?page=<bar>"
RewriteRule ^(games/[\w-] )/([\w-] )/?$ pages/$1/?page=$2 [L,NC,QSA]

# Rewrite "/games/<anything>" to "/pages/games/<anything>"
RewriteRule ^games($|/.*) pages/$0 [L,NC]
  • Related