1

URL coming into Apache:

localhost:8888/game?level=0

URL that should be coming out of apache:

localhost:8888/level0/game

Could someone kindly help me to create this rewrite ?

Tried to solve it with following:

RewriteEngine On
RewriteRule ^game\/+(.*?)\?+level=+([0-9]+) /level$2/$1 [L,QSA]

With no luck since it does not match.

Thanks, Peter

4
  • 2
    You would get more help if you provide what you have attempted to solve this. Commented Feb 25, 2015 at 14:25
  • @hwnd Added the solution I have tried Commented Feb 25, 2015 at 14:30
  • 1
    Are you sure you want that direction of rewriting (making something static look dynamic?) most often rewrite rules are written the other way around. Anyway, in RewriteRule you will be able to match just the url path (with no query string). You need RewriteCond %{QUERY_STRING} ..... to capture query string values, see wiki.apache.org/httpd/RewriteQueryString Commented Feb 25, 2015 at 14:35
  • @guido yep, this is the correct direction. So, adding RewriteCond %{QUERY_STRING} game still does not fix it.. Could you kindly seuggest firther direction ? Commented Feb 25, 2015 at 14:39

2 Answers 2

1

To be able to capture values from the query string, you need a RewriteCond %{QUERY_STRING} directive, so that captured group will be available as %n placeholders in the following RewriteRule:

RewriteCond %{QUERY_STRING} ^level=(.*)$
RewriteRule ^game /level%1/game [L]

If you type /game?level=0 in the browser, apache will open /level0/game; no need to append the query string to the rewritten url (QSA flag)

Sign up to request clarification or add additional context in comments.

9 Comments

This does not seem to work, when I copy paste these settings and open start url I am getting Not Found The requested URL /game was not found on this server. Works with ^game
@Peter what is the start url?
and does a /level0/game file exist under the document root for that context?
@Peter ah sorry, I missed the last Works with ^game part of your first comment, I had a typo
@anubhava yep he got unusual requirement, as you can see I made sure asking in a comment below the question
|
1

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /(game)\?(level)=(\d+) [NC]
RewriteRule ^ %2%3/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^(level)(\d+)/([^/.]+)/?$ $3?$1=$2 [L,QSA,NC]

Comments

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.