rewriting hard coded links

Topics: User Forum
Oct 6, 2008 at 4:19 PM
Edited Mar 13, 2009 at 8:01 PM
if the following link is hard coded in the aspx pages.
http://fcp.com/ViewCart.aspx
and I have a filter like this:
# http://fcp.com/cart/ --> http://fcp.com/ViewCart.aspx
ReWriteRule ^(.*)/cart/? $1/ViewCart.aspx [I,U]

the url is not corrected to http://fcp.com/cart/
if the hard coded link is followed.

but if the user types fcp.com/cart/, the correct page displays.

how do you guys get around ensuring that the clean url always shows.

It seems like I could easy create an endless loop by having a second rule that
changes # http://fcp.com/ViewCart.aspx --> http://fcp.com/cart/
ReWriteRule ^(.*)/ViewCart.aspx $1/cart/ [I,U]

(that actually breaks both links)

Thanks.

Coordinator
Oct 6, 2008 at 8:36 PM
Edited Mar 13, 2009 at 11:03 PM

The business of rewriting and redirecting is confusing, and one of the trickiest parts is often just communicating what you want to do, or from my perspective, understanding what people want to do. I'm sorry I don't quite get it from your description so I'm going to ask some questions.

You have a filter like this:

# http://fcp.com/cart/ --> http://fcp.com/ViewCart.aspx
ReWriteRule ^(.*)/cart/? $1/ViewCart.aspx [I,U]

This says, if someone types in http://fcp.com/cart/, then rewrite (server side only) to http://fcp.com/ViewCart.aspx . In other words, the ViewCart.aspx page runs, and displays. The URL address bar in the browser never changes from http://fcp.com/cart/. This is your basic Rewrite. As I have said countless times and as it is described in the readme, the rewrite happens only on the server side. The browser is none the wiser that a rewrite occurred.

You then wrote, the url is not corrected to http://fcp.com/cart/ if the hard coded link is followed.But the rule you gave, does not "correct" any URL to http://fcp.com/cart/ . Your rule goes the other way.

Listen, the other thing is, people have a way of understanding what the rewrite engine is doing - Some people think of it as "search engine optimization" and you described it as "correcting URLs". Other people describe it differently. But I don't know what you mean when you say "the URL is not corrected to http://fcp.com/cart/ ." We can do much better if we stick to the agreed-upon language of URL rewriting. REWRITE and REDIRECT have a clear, well-defined meaning. It may be that you are not clear on the meaning, but the meaning is clear and fixed. "CORRECTED URL" is not clearly defined. when you use the non-standard language, You know what you mean, but I don't.

When you say, "the URL is not corrected", Do you mean, "the URL is not rewritten" ? Do you mean "the URL is not redirected"? Do you mean something else? And if you mean rewritten or redirected, then what is the URL to which you think the original should have been rewritten to, or redirected to? Again, your rule rewrites only to ViewCart.aspx, and will never rewrite to http://fcp.com/cart/ , and will never redirect to *anything*.

You also asked how do you guys get around ensuring that the clean url always shows. When you say "shows" I can guess that you mean it shows in the URL address bar in the browser. But I'm not sure. If that is what you want then you need a REDIRECT. You should report that the URL is not REDIRECTED, if that is what you see.

I'd ask that you use the parlance. Say REWRITE when you mean REWRITE. Say REDIRECT when you mean REDIRECT. Don't say "The URL is not CORRECTED."  That does not make it clear at all.  If you stick to the language of URL rewriting, I will be more able to help you .

If you don't understand the difference between Redirect and Rewrite, you should check the readme again.

 

Oct 6, 2008 at 9:08 PM
Edited Mar 13, 2009 at 8:01 PM
I want http://fcp.com/cart/ to be the only visible url in the browser address bar
for this file: http://fcp.com/ViewCart.aspx

if someone navigates to http://fcp.com/ViewCart.aspx
I want the url to be rewriten to http://fcp.com/cart

they could navigate to http://fcp.com/ViewCart.aspx either by
clicking a hard coded link on our site (a href = http://fcp.com/ViewCart.aspx)
or by typing it directly into the browser address bar. I can not control the hard coded links on our
site.  they are all dynamically generated through code files that I do not have access to modify.

so, if the user clicks a link that says "view your cart", the html behind that link will be
a href = http://fcp.com/viewcart.aspx

as is, clicking the "view your cart" link rewrites the url to
http://fcp.com/viewcart.aspx

but I want the address to be immediately rewritten to:
http://fcp.com/cart/
so that any bookmarking, linking, or telling friends about our site will result in
http://fcp.com/cart/ being the url that is recorded or passed on to friends.

then, if a visitor clicks a link or types into their browser address bar:
http://fcp.com/cart/
I want that url to be redirected to
http://fcp.com/viewcart.aspx (but I do not want the address bar url to be rewritten)

sorry for the confusion - and I hope this is more understandable.

thank you.

Coordinator
Oct 6, 2008 at 10:00 PM
Edited Oct 6, 2008 at 10:02 PM

Yes, it is becoming clearer.

you wrote:

but I want the address to be immediately rewritten to:
http://fcp.com/cart/
so that any bookmarking, linking, or telling friends about our site will result in
http://fcp.com/cart/ being the url that is recorded or passed on to friends.

You want a REDIRECT.  You are using the word rewritten, but you do not want a REWRITE.  You want REDIRECT.

Check the readme to clarify the difference between a rewrite and a redirect.  Please, please, please - read it again.  Make sure you understand the difference.  It's critical, otherwise all of our back-and-forth on this issue will continue to be confused.

To get a REDIRECT you need to use the [R] flag.

Here is the catch: while you want the URL to show in the browser bar to be http://fcp.com/cart/ , it seems to me that you want the ViewCart.aspx page to actually run.

So you have the situation where if someone clicks on http://fcp.com/ViewCart.aspx , then you want to REDIRECT to http://fcp.com/cart, but then.... (ready for this?) you want to REWRITE that request to ViewCart.aspx. 

As I said before, the REWRITE does not change the URL in the browser address bar. The REWRITE is a server-side operation.  It merely changes what gets executed on the server side.

Here's what I mean:

# REDIRECT to change the address in the browser address bar
RewriteRule ^/ViewCart.aspx$   /cart   [R]
# REWRITE so that the correct page executes when /cart is the URL
RewriteRule ^/cart$  ViewCart.aspx   [L]
Coordinator
Oct 6, 2008 at 10:08 PM
After thinking about this, I can see that I am encouraging confusion on this.
The ini file uses RewriteRule whether it is a rewrite or a redirect.  It shouldn't. 

I should fix the filter to use the directive RedirectRule when doing a Redirect.  That would fix half the problem. 

By using the RewriteRule directive for redirects, I am only encouraging confusion.

(In my defense, the syntax comes from mod_rewrite.  I just perpetuated it. )
Oct 6, 2008 at 10:11 PM
Edited Mar 13, 2009 at 8:03 PM
I had to make one small change, but this works perfectly.
# REDIRECT to change the address in the browser address bar
RewriteRule ^/ViewCart.aspx$   /cart/   [R]
# REWRITE so that the correct page executes when /cart is the URL
RewriteRule ^(.*)/cart/?$  $1/ViewCart.aspx   [L]

had to grab the negative lookahead and recall the value with $1.

I think this explains a lot.
I'll dig deeper into our site now and see how much I can tweak myself.

I really appreciate your thoughts and time.