Url rewriting mod_rewrite with and without query string - apache

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?

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...

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/

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

Related

How do you ignore parts of a URL with Mod Rewrite?

I am using Mod Rewrite to create attractive URLs. I am trying to understand how it is possible to ignore parts of a URL?
For example, my original URL was:
/file.php?token=771736eb78bc3b6f96d5c066069567c0c219bf65/
And I have written a Mod Rewrite code to turn that into:
/file/771736eb78bc3b6f96d5c066069567c0c219bf65/
RewriteEngine On
RewriteRule ^file/([^/]*)$ /file.php?token=$1 [L]
However, I would like to achieve a URL like:
/file/771736eb78bc3b6f96d5c066069567c0c219bf65/random-bits-here/
The random bits on the end serve no purpose (other than decoration). How do I tell Mod Rewrite to ignore this part of the URL?
Just tweak your regex like this
RewriteRule ^file/([^/]+)/?(.*)$ /file.php?token=$1 [NC,L]
You should add the second group of characters to your RewriteRule and ignore the caught expression, if you don't need it.
RewriteEngine On
RewriteRule ^file/([^/]*)/([^/]*)/$ /file.php?token=$1 [L]
Or, if you would like to have it passed to your script.:
RewriteEngine On
RewriteRule ^file/([^/]*)/([^/]*)/$ /file.php?token=$1&extra=$2 [L]
Simply leave out the $ sign, which forces the end of the string (path):
RewriteEngine On
RewriteRule ^file/([^/]*) /file.php?token=$1 [L]

first rewrite rule overriding the rest

quick question for those who know a little more about mod-rewrite than i do:
I have some rules written in sequence like so:
RewriteRule ^(en|fr) index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/home index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/terms index.php?page=terms&lang=$1 [L]
however the first one seems to be over-riding the rest.
I've tried taking off the [L] but this doesnt do what I expected and it will never show the "terms" page.
Any ideas?
If you want http://www.example.com/en or http://www.example.com/fr/ (with or without trailing slash) to redirect to the first item, change the first rule to be:
RewriteRule ^(en|fr)/?$ index.php?page=home&lang=$1 [L]
The /? matches whether or not there's a trailing slash and the $ means "match the end of the URL" (don't match if the URL continues).

RewriteCond - how to escape this?

Hi am having a problem rewriting this url - how do I escape it so it will correct itself?
The url was sent out into the public domain like so:
http://www.domain.com/directory/directory-2/10018485&key=3945
I need to write a rule to correct it:
http://www.domain.com/directory/directory-2/10018485?key=3945
`
Am tring to do it like so, without success....
RewriteCond %{REQUEST_URI} \/directory\/directory-2\/10018485&key=3945
RewriteRule ^(.*)$ /directory/directory-2/?id=10018485&key=3945 [L]
Thanks :)
You don't need to escape the & in the match to %26. What you have works fine for me, not sure why it isn't working for you. But you don't need a RewriteCond, you can combine the 2 into a single rule. These work for me when the actual request is /directory/directory-2/10018485&key=3945 or directory/directory-2/10018485%26key=3945
RewriteRule ^directory/directory-2/10018485&key=3945$ /directory/directory-2/?id=10018485&key=3945 [L]
In a more abstract sense:
RewriteRule ^directory/directory-2/([0-9]+)&key=([0-9]+)$ /directory/directory-2/?id=$1&key=$2 [L]

avoid dir mod rewrite

i have a rule like this:
RewriteRule ^posts/(.*)/([0-9]*)$ viewupdates.php?username=$1&page=$2 [L]
and match to: http://site.com/posts/username/1
i need to chante to:
http://site.com/username/posts
without trailing slash, and if have more pages /posts/1
Simply transpose the corresponding parts of the regexp for the latter case:
RewriteRule ^(.*)/posts/([0-9]*)$ viewupdates.php?username=$1&page=$2 [L]
Probably, you also want another rule without the extra "page" specification (for the former case you describe), smth like this:
RewriteRule ^(.*)/posts$ viewupdates.php?username=$1 [L]
or
RewriteRule ^(.*)/posts$ viewupdates.php?username=$1&page=1 [L]
depending on your implementation of the script.

mod_rewrite weird problem

I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom