blacksnday Posted January 1, 2007 Share Posted January 1, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted January 2, 2007 Share Posted January 2, 2007 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 Link to comment Share on other sites More sharing options...
blacksnday Posted January 2, 2007 Author Share Posted January 2, 2007 [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 asSEO 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 2, 2007 Share Posted January 2, 2007 How are you differentiating between viewing with and without pagination? Can you type up some examples of valid URLs? Quote Link to comment Share on other sites More sharing options...
blacksnday Posted January 2, 2007 Author Share Posted January 2, 2007 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]. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 2, 2007 Share Posted January 2, 2007 Thanks. I thought I was confused for a moment, but it looks like you're all set--correct? Quote Link to comment Share on other sites More sharing options...
blacksnday Posted January 2, 2007 Author Share Posted January 2, 2007 [quote author=effigy link=topic=120531.msg495546#msg495546 date=1167766695]Thanks. I thought I was confused for a moment, but it looks like you're all set--correct?[/quote]Yes, all set now.Thanks for the advice! Quote Link to comment Share on other sites More sharing options...
blacksnday Posted January 2, 2007 Author Share Posted January 2, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 2, 2007 Share Posted January 2, 2007 What about your original regex with mine applied?[tt]RewriteRule info/(.*)/(.*).html/(?:(.*)\.html)? index.php?id=$1&title=$2&cpage=$3[/tt] Quote Link to comment Share on other sites More sharing options...
blacksnday Posted January 2, 2007 Author Share Posted January 2, 2007 [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. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 2, 2007 Share Posted January 2, 2007 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? Quote Link to comment Share on other sites More sharing options...
blacksnday Posted January 2, 2007 Author Share Posted January 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted January 3, 2007 Share Posted January 3, 2007 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] Quote Link to comment Share on other sites More sharing options...
blacksnday Posted January 3, 2007 Author Share Posted January 3, 2007 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 issuesbut 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.