Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Question: Adding a ?page=xxx to end of URL

And rather stuck...

         

Karma

3:12 pm on Jan 22, 2015 (gmt 0)

10+ Year Member



I currently have the following working:

/mydomain.tld/keyworda/keywordb/parma/parmb

Using:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?keyworda=$1&keywordb=$2&parma=$3&parmb=$4 [L]


However, I'd like to add an optional page parameter at the end:

/mydomain.tld/keyworda/keywordb/parma/parmb?page=3

I've tried everything I can think of to get this to work, but I'm out of ideas.

Currently using (which doesn't work):

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?keyworda=$1&keywordb=$2&parma=$3&parmb=$4&parmc=$5 [L]


Can someone help? :/

penders

4:48 pm on Jan 22, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You just need to include the QSA (query string append) flag on the RewriteRule. ie. [QSA,L]

This will add the passed query string to the end of the query string in the RewriteRule substitution.

lucy24

8:40 pm on Jan 22, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<tangent>
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?keyworda=$1&keywordb=$2&parma=$3&parmb=$4 [L]

Change each * to + to make sure mod_rewrite doesn't treat accidental double slashes // as separate directories. (It may not anyway, but it can't hurt to be safe.)

/?$

This implies that the same page can be reached both with and without final / slash. Pick one and stick with it.
</tangent>

By default, a ? (with or without following query string) in the RewriteRule target will overwrite/replace/delete any pre-existing query string. That's what the [QSA] flag is good for; it means "hold on to any parameter that's already present". Just make sure you don't end up with the same parameter twice. Here it looks as if that won't be a problem.

Karma

11:58 am on Jan 26, 2015 (gmt 0)

10+ Year Member



Thanks both, have been playing around with this but still not working... the QSA flag didn't seem to make a difference.

As mentioned before, I'm trying to get the value of 'page'.

mydomain.tld/aaa/bbb/ccc/ddd?page=2

This is my new rule as advised above:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /index.php?keyworda=$1&keywordb=$2&parma=$3&parmb=$4&pageid=$5 [QSA,L]

I've looked at other sources, and from what I understand this should be working?

Karma

12:10 pm on Jan 26, 2015 (gmt 0)

10+ Year Member



Ok, looks like I'm confused.

If I change the &pageid=$5 to &page=$5 then it works as this matches the URL parameter.

I always thought that this worked by finding a match using in the structure defined on the left, then rewriting it as the structure on the right. Why can't I rename the variable $5 to whatever I wish!? :/

Thanks

penders

12:19 pm on Jan 26, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Errm, what does $5 refer to? In the example you have posted I don't see how it matches the URL "page" parameter?

You have 4 parenthesised sub patterns in your RewriteRule pattern, but you are referencing $1..$5 in the RewriteRule substitution? Shouldn't $5 be empty, undefined (or something)?

Shouldn't it be:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /index.php?keyworda=$1&keywordb=$2&parma=$3&parmb=$4 [QSA,L]


Unless you need the "pageid" param as well?

[edited by: penders at 12:26 pm (utc) on Jan 26, 2015]

Karma

12:24 pm on Jan 26, 2015 (gmt 0)

10+ Year Member



Things are starting to become clearer :/

I thought the $ on the left pattern referred to the &pageid=$5

*sheepish grin*

penders

12:29 pm on Jan 26, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The $ at the end of the RewriteRule pattern is simply the end-of-string placeholder. In other words, nothing can come after the 4th path segment in the URL. (Just like ^ signifies the start of the string/URL.)

The query string (ie. ?page=2) is not part of the pattern match.

Karma

12:35 pm on Jan 26, 2015 (gmt 0)

10+ Year Member



Got it :)

As I'm working with an old site/rules, am I able to change the name of the ?page=2 parameter so that my PHP sees "pageid" instead of "page"?

penders

12:56 pm on Jan 26, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, in that case you do need to manually specify the pageid param in the RewriteRule substitution. Something like:

RewriteCond %{QUERY_STRING} ^page=([0-9]{1,2})
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /index.php?keyworda=$1&keywordb=$2&parma=$3&parmb=$4&pageid=%1 [L]


The QSA flag is now unnecessary. The %1 refers to the first parenthesised sub pattern in the RewriteCond directive above.

Oh, the "page" parameter is entirely optional?

Karma

1:06 pm on Jan 26, 2015 (gmt 0)

10+ Year Member



Yes, it's optional.

I've made a work-around now regarding renaming the page parameter.

Thanks for the help, learnt a few things today!

EDIT: The only issue with my rewrite now is 'page' is always set, but I can always check to see if it's null instead.

penders

2:02 pm on Jan 26, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The only issue with my rewrite now is 'page' is always set... but I can always check to see if it's null instead.


I assume you mean "pageid" is always set? Is that a problem? If it's always set then you potentially don't need to check for it's existence ;) - although you should.

TBH, the check is the same whether the pageid param is present and is empty or is omitted entirely...

if (empty($_GET['pageid'])) // pageid is omitted, set to empty string or is '0'


$_GET['pageid']
is never NULL under normal conditions. It is either not set if omitted or is set to an empty string if passed with no value.

To make the "page" parameter entirely optional in the request:

RewriteCond %{QUERY_STRING} ^(?:page=([0-9]{1,2}))?


The ?: after the opening parenthesis makes it a non-capturing subpattern (just so you don't have to change the %1 in the RewriteRule substitution). And the trailing ? makes the whole thing optional. (Not tested.)

Karma

2:09 pm on Jan 26, 2015 (gmt 0)

10+ Year Member



Great, thanks Penders :)