Basic url rewrite withRewrite Rules Module

Topics: General, Troubleshooting, Writing themes
Dec 21, 2011 at 2:47 PM

Hi,

I'm trying to redirect the url "http://www.mysite.com/foo" to "http://www.mysite.com/foo/sub".

I have successfully gotten "foo" to redirect to "bar" (as a test) as follows:

RewriteRule foo bar

So the module seems to be working. I think it's my rewrite syntax that is incorrect. When I write: 

RewriteRule foo foo/sub

...I get the following error:

HttpException (0x80004005): The length of the URL for this request exceeds the configured maxUrlLength value

 

Without the rewrite rules enabled, directly accessing http://www.mysite.com/foo/sub works properly. So I'm not sure what's wrong with my second example, but it's not working

Thanks for any help.

-Tim

Dec 21, 2011 at 3:16 PM

The problem is the rewrite engine will keep running the rewrites over and over until it's told to stop. You're creating a recursive rewrite like this:

foo

foo/sub

foo/sub/sub

foo/sub/sub/sub

etc.

So eventually it's hitting the length limit.

Change your rule to:

RewriteRule foo foo/sub [L]

The [L] means "last", telling the engine to stop processing.

Dec 21, 2011 at 3:34 PM
Edited Dec 21, 2011 at 4:00 PM

Guess I need to brush up on my mod_rewrite :-)

Needed to add one addition (the trailing $) to what you suggested:

RewriteRule foo$ foo/sub [L]

 

So [L] tells engine to stop processing, and I'm guessing $ (regex "end") does something similar for the pattern. Once again, randompete to the rescue. Thanks!

Dec 30, 2011 at 3:03 PM

A quick follow up:

I can't get the second url to work.

Urls to test (Simulations tab):
http://mysite/foo
http://mysite/foo/bar

Rules:

RewriteRule ^foo$ foo/bar/zap [L]
RewriteRule ^foo/bar$ foo/bar/zap [L]

The idea is that I need both urls to resolve to foo/bar/zap. Each rewrite rule works INDIVIDUALLY, but with both of them in there, only the first one works (foo resolves to foo/bar/zap, but foo/bar does not). 

Thanks, again.

-Tim

Coordinator
Dec 30, 2011 at 5:18 PM

It seems the version on the gallery is not the latest one, fixing this issue and a bunch of others. Let me update it.

Coordinator
Dec 30, 2011 at 5:24 PM

The new version is online, you rule seems good and should work with the update.

Dec 30, 2011 at 6:03 PM

Thanks so much for the update, sebastien. Is there something I need to do to ensure that I have the latest? I just tried: 1) installing from Gallery via Admin, and 2) downloading and then "Install module from computer" option (Nuget package). (also did Clean and Rebuild of Solution)

The "Simulate" tab indicates that these rules will resolve to the foo/bar/zap url correctly. Both indicate "Transfer to foo/bar/zap".

However, actually navigating to foo/bar takes me to foo/foo/bar/zap

Dec 30, 2011 at 8:25 PM

In other words, it's still not working for me.

Coordinator
Dec 30, 2011 at 9:18 PM

Check the Module.txt file, it should be 1.2.4

Jan 2, 2012 at 9:12 PM

I verified that I have the latest code (version of the Module.txt is 1.2.4). 

Here's what I'm noticing: 

http://orchard-dev/headquarters takes me to 
http://orchard-dev/headquarters/green-components/geothermal-hvac-system 
(which is correct)

http://orchard-dev/headquarters/green-components tries to take me to 
http://orchard-dev/headquarters/headquarters/green-components/geothermal-hvac-system 
(notice the repeated "headquarters" in last url)

Here are my rules:

RewriteRule /headquarters$ headquarters/green-components/geothermal-hvac-system [L]
RewriteRule /headquarters/green-components$ headquarters/green-components/geothermal-hvac-system [L]

I honestly don't know whether this is the Module code or just something wrong w/ my rules.