0

I'm trying to rewrite from domain.com/page/soft-15/android-26/ to page.php?cat=15&os=26 with this code:

RewriteRule ^page/([0-9]+)\-([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)$ page.php?cat=$1&os=$2

I thinks it works fine but, how can i rewrite domain.com/page/ and domain.com/page (without the last forward slash) to domain.com/page.php keeping both rules working?

3 Answers 3

1

ok think i got it mixing Jimp & Jon code

RewriteRule ^page/?$ page.php [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1&os=$2 [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1 [L]

This math with

domain.com/page
domain.com/page/
domain.com/page/soft-15
domain.com/page/soft-15/
domain.com/page/15
domain.com/page/15/
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/soft-15/26
domain.com/page/15/26
and so on...
Sign up to request clarification or add additional context in comments.

Comments

0

Add a trailing /? to your pattern (before the $ anchor). The ? makes the / optional.

Additionally, your patterns seem backward, matching the digits before the characters. Try this:

RewriteRule ^page/(?:([a-zA-Z\d-]*)-)?(\d+)/(?:([a-zA-Z\d-]*)-)?(\d+)/?$ page.php?cat=$1&os=$2

That should match these variations like these:

domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/15/android-26
domain.com/page/15/android-26/
domain.com/page/soft-15/26
domain.com/page/soft-15/26/
domain.com/page/15/26
domain.com/page/15/26/

4 Comments

with my code (with or without your trailing ) trying to type domain.com/page/ (or domain.com/page ) just print the requested URL was not found on this server
I didn't look at your pattern closely before because you said it is working fine, but I don't think that is possible. Your [match characters]-[match digits] patterns are swapped up. I'll take another crack at it.
as #Jon Lin said, my/your code rewrites to page.php?cat=soft&os=15
yes, finally i got what i want, the code is just below my own question
0

Your backreferences don't seem to jive with the example that you're using:

  • /page/soft-15/android-26/
  • to page.php?cat=15&os=26

Your regex looks like it's matching:

  • /page/15-soft/26-android/

And rewriting to:

  • page.php?cat=15&os=soft

If you're going by your example, you'd want something like:

RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1&os=$2
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1
RewriteRule ^page/?$ page.php

2 Comments

You are right, I made a mistake with the code, your code works fine but anyway what i was looking for is to work in all condition, with 2 query , page/soft-15/android-26, with 1 query , page/soft-15 or with no query string at all, page/
@Colas Then you need a rule for each one. See the edited answer

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.