2

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.

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thank you, man. It works perfectly. I wanted to add that I discovered this RewriteRule of mine RewriteRule ^([^/]*)/([^/]*)$ $1/index.php?page=$2 [L,QSA] also works if you put it in a new .htaccess file located directly inside the game subfolder. It would be here: /pages/games/.htaccess. I just wanted people to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.