Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule to fix printer url

         

JamesTrix

5:07 am on Apr 27, 2010 (gmt 0)

10+ Year Member



I have a nice option in my blog to print a story but when printing, the url is appearing at the top of the page. I can not turn this off at the server side even with javascript as this can be only done by the users browser or so Google tells me.

As the url is very long I wanted to some how rewrite this to a shorter nicer url just so it looks nicer on the printed page.

This is the current url

/wp-content/themes/theme/_print.php?cid=10

If I was to just rewrite the url to my domain name would that cause my default page to be displayed ?

If not could I just rewrite it to domainname/printed

Your ideas on the above issue would be great :-)

g1smd

7:10 am on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rewriting works with URL requests to fetch content from a non-default location in your server.

If you want users to see a shorter URL, that is the URL you will need to link to in your "print this" link.

A rewrite can then silently fetch the content from the real server-internal location for this content.

This might be a good time to review the entire URL structure for your site.

If you can link to "
/print/10
", for example, on the pages of your site, then this would likely work:

RewriteRule ^print/([0-9]+)$ /wp-content/themes/theme/_print.php?cid=$1 [L]

JamesTrix

12:10 am on Apr 28, 2010 (gmt 0)

10+ Year Member



Sorry I was trying to do it the other way around I see the error in my design.

Mod rewrite might not be the perfect fix I am trying to create a url with out a ID number. Not sure if I could post to a php page some how so that it does not show the page id.

jdMorgan

2:44 am on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The main thing to grasp is that the only way to "change a URL" is to change the link on your page itself.

In this case, that means modifying WP to output a "prettier" link.

Once that is done, you can "accept" that prettier linked URL using mod_rewrite on your server, and "connect" it back to the correct internal-filepath+query-string needed to actually serve the content.

mod_rewrite works as an HTTP request arrives at your server, and changes the URL-to-filename mapping for that HTTP request. It is not an "on-the-fly page output editor" and cannot change the links on your pages that people click on.

The problem must be fixed on the page itself, so that the "print this" link is shorter/prettier.

However, this shorter/prettier link must be unambiguous and it must contain sufficient information for your server (e.g. mod_rewrite) to 're-build' the original ugly long URL-path, so that it can serve the correct content.

Jim