Jump to content

[SOLVED] regex conditional howto?


blacksnday

Recommended Posts

This is being used inside Apache .htaccess but figured I would post here since it is regex.
How would the following be changed, in order to allow a conditional check?
[code]RewriteRule info/(.*)/(.*).html/(.*).html index.php?id=$1&title=$2&cpage=$3[/code]
this would make a url such as:
[code]links/info/16/World-of-Warcraft-Rocks.html/0.html[/code]

What I want to be conditional would be the last two parts...
[code]title=$2&cpage=$3[/code]
If there is no [b]cpage[/b] in the url, it would fail as written, but I want it to succeed if or if not [b]cpage[/b] is included
Link to comment
Share on other sites

Try changing[tt] (.*).html [/tt]to[tt] (?:(.*)\.html)?[/tt]. The period is a metacharacter, so it must be backslashed to be taken as a literal.[tt] (?: ) [/tt]is used to group without capturing, and the[tt] ? [/tt]following it makes it optional.
Link to comment
Share on other sites

[quote author=effigy link=topic=120531.msg495439#msg495439 date=1167759806]
Try changing[tt] (.*).html [/tt]to[tt] (?:(.*)\.html)?[/tt]. The period is a metacharacter, so it must be backslashed to be taken as a literal.[tt] (?: ) [/tt]is used to group without capturing, and the[tt] ? [/tt]following it makes it optional.
[/quote]

Based on your suggestions I changed it to the below, which works now exactly as needed.
In my situation, I needed this to work to allow for article selection and viewing, as well as
SEO friendly comment pagination if comments are available and the max view is exceeded.

How does this look now?
[code]RewriteRule in-(.*)-(?:(.*))-(?:(.*)\.html)? index.php?id=$1&title=$2&cpage=$3[/code]
This now allows an optional .html extension(when viewing article without comment pagination)
and one required .html(default for comment pagination extension),
as well as optional title/cpage values.
Link to comment
Share on other sites

Sure!

Based on calling:
[b]index.php?id=$1&title=$2&cpage=$3[/b]

URL on selected article without comment pagination:
[b]http://localhost/links/in-16-World-of-Warcraft-Rocks.html[/b]
(the .html on this link is now optional and can be omitted when the below pagination link is called,
thanks to the advice and changes in the .htaccess RewriteRule)

URL on selected article with comment pagination:
[b]http://localhost/links/in-16-World-of-Warcraft-Rocks/page-1.html[/b]
The [b]/page-1.html[/b] can also use [b]-page-1.html[/b].
Link to comment
Share on other sites

I just realized, that doing it this way:
[code]RewriteRule in-(.*)-(?:(.*))-(?:(.*)\.html)? index.php?id=$1&title=$2&cpage=$3[/code]

Is causing the server global $_GET to jumble and mix id,title,cpage within eachother.
when changing it to
[code]RewriteRule in=(.*)/title=(?:(.*))/cpage=(?:(.*)\.html)? index.php?id=$1&title=$2&cpage=$3[/code]
of course it messes it up and wont work as hoped for, however the server $_GET is just fine now.
Hmmm, more work is needed by me on this.
Link to comment
Share on other sites

[quote author=effigy link=topic=120531.msg495590#msg495590 date=1167770907]
What about your original regex with mine applied?

[code]RewriteRule info/(.*)/(.*).html/(?:(.*)\.html)? index.php?id=$1&title=$2&cpage=$3[/code]
[/quote]

The problem with that is with[b].html/[/b]
as it is always required otherwise it fails.
How do I make the [b].html[/b] and the [b]/[/b] seperate and each optional?
That would solve my problems.
Link to comment
Share on other sites

Another repeat of[tt] (?:(.*)\.html)? [/tt]will make the html portion optional, and[tt] /? [/tt]will make the slash optional, resulting in:

[tt]info/(.*)/(?:(.*)\.html)?/?(?:(.*)\.html)?[/tt]

I'm not sure this is what you want though, because there's so many optional pieces. Going back to my request, can you show more examples, including all of the combinations?
Link to comment
Share on other sites

Here is all the combinations and url styling I am looking to achieve/allow:
(this is a little different then earlier posted, because I am attempting a clean url scheme)

/in=16/title=World-of-Warcraft-Rocks
/in=16/title=World-of-Warcraft-Rocks.html
/in=16/title=World-of-Warcraft-Rocks.html/
/in=16/title=World-of-Warcraft-Rocks/page=1.html
/in=16/title=World-of-Warcraft-Rocks/1.html
Link to comment
Share on other sites

Why use the "in=", "title=", and "page=" if the positions do not change? Try this; I made an example in PHP to show the capturing:

[code=php:0]
<?php

$tests = array(
'in=16/title=World-of-Warcraft-Rocks',
'in=16/title=World-of-Warcraft-Rocks.html',
'in=16/title=World-of-Warcraft-Rocks.html/',
'in=16/title=World-of-Warcraft-Rocks/page=1.html',
'in=16/title=World-of-Warcraft-Rocks/1.html'
);

foreach ($tests as $test) {
if (preg_match('%in=(\d+)/title=(.+?)(?:$|\.html/?|/(?:page=)?(\d+)\.html)%', $test, $matches)) {
echo '<hr>', $test, '<br>', '<pre>', print_r($matches, true), '</pre>';
}
}

?>
[/code]
Link to comment
Share on other sites

I want to use "in=", "title=", and "page=" simply for ease.
Eventually I plan to write a full URL Rewrite class that would address several issues
but for now I am just doing it all through .htaccess.

Your php example is great and also brought me to some other ideas for uses of it.
This problem is fully solved now, and I thank you for spending the time teaching me a few things!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.