Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Regex Router Flags
 
s1ntax
Jr. Member
 
Total Posts:  6
Joined:  2009-01-22
 

So I’ve been trying to do regex rewrites with at least two different rules under the same module frontName; here is my config.xml

<rewrite>
            <
career-job-pages>
                <
from><![CDATA[/island-careers\/([a-z]*)\/([a-z]*)\/([a-z]*)/]]></from>
                <
to><![CDATA[island-careers/departments/jobdescription/location/$1/department/$2/job/$3/]]></to>
                <
complete>1</complete>
                <
last>1</last> <!-- my new feature request -->
            </
career-job-pages>
            <
career-departments>
                <
from><![CDATA[/island-careers\/([a-z]*)/]]></from>
                <
to><![CDATA[island-careers/departments/index/department/$1/]]></to>
                <
complete>1</complete>
                <
last>1</last>  <!-- my new feature request -->
            </
career-departments>
        </
rewrite>

here is Mage_Core_Controller_Varien_Front::rewrite() which I have done a temporary core modification to until I can find time to do this a better way. (never ever do core modifications!) I’ve modified the end of the method to include handling for my <last /> tag in the config.xml

foreach ($config->children() as $rewrite{
            $from 
= (string)$rewrite->from;
            
$to = (string)$rewrite->to;
            if (empty(
$from) || empty($to)) {
                
continue;
            
}
            $from 
$this->_processRewriteUrl($from);
            
$to   $this->_processRewriteUrl($to);
            
            
$pathInfo preg_replace($from$to$request->getPathInfo());
            
            if (isset(
$rewrite->complete)) {
                $request
->setPathInfo($pathInfo);
            
else {
                $request
->rewritePathInfo($pathInfo);
            
}
            
if($pathInfo != $request->getPathInfo() &&   // if it actually rewrote something
               
isset($rewrite->last)) break;                         // and the last flag is set, dont continue rewriting

        
}

So the idea I’m going for is kind of like an [L] in .htaccess rewrites where basically, if it finds a match it wont continue rewriting. because right now it just execute both of my rewrite rules and ends up not giving me what i want.

I’m sure there is a better way to write the regex (my regex-foo is not as strong as I’d like it to be), or I could use my own router but this really seems like the simplest solution for what I’m trying to do. I’ve tried containing my regex with start-end anchors: /^ $/ but it breaks…

Anyway, thats my idea for a feature addition to Regex rewrites!

cheers,
Sean

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top