0

I have reviewed similar questions on SO and cannot find one that fits my needs so this is not a duplicate.

I have a simple php page called top.php. I pass a parameter in the url like so: top.php?n=100.

In my .htaccess file I use mod_rewrite to change the url to top-100. Simple. But what I want to do is not have to hard code every possible url for any number passed as a parameter. Let me elaborate:

top.php?n=5 | rewrite to top-5

top.php?n=3 | rewrite to top-3

top.php?n=10 | rewrite to top-10

And so on.

So how do I create a rewrite using the query string parameter to dynamically generate the friendly url?

1
  • try a regular expression, something like top.php\?n=([0-9]+) | top-$1 Commented Nov 7, 2016 at 19:40

1 Answer 1

1

You can use these rules in your .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} /top\.php\?n=(\d+) [NC]
RewriteRule ^ /top-%1? [R=301,L,NE]

RewriteRule ^top-(\d+)/?$ top.php?n=$1 [L,QSA,NC]
Sign up to request clarification or add additional context in comments.

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.