Print Friendly Variable

Discuss practical ways rearrange URLs using mod_rewrite.

Print Friendly Variable

Postby Ulysses » Thu Sep 18, 2008 8:08 am

Hi there,

I'm using something along the lines of the following to provide a print-friendly link on my pages:
/category/product.html?print=true
/category/?print=true

Can anyone guide me as to whether or not I could rewrite the URL to something friendly like the following:
/print/category/product.html
/print/category/

Look forward to your comments/suggestions.
Last edited by Ulysses on Fri Sep 19, 2008 3:36 am, edited 2 times in total.
Ulysses
 
Posts: 10
Joined: Thu Sep 18, 2008 7:58 am

Postby Ulysses » Fri Sep 19, 2008 12:19 am

Am I even close?

Code: Select all
RewriteRule ^/print/(.*)$ ?print=yes [QSA,L]
Ulysses
 
Posts: 10
Joined: Thu Sep 18, 2008 7:58 am

Postby richardk » Fri Sep 19, 2008 7:29 am

Yes, try the following in a .htaccess file in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^print/(.+)$ /$1?print=yes [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Ulysses » Fri Sep 19, 2008 8:13 am

Hi Richard,

Sadly it isn't working ... perhaps it is because I am already using a few conditions in my htaccess file already?

Code: Select all
AddType application/x-httpd-php .html

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
AddDefaultCharset UTF-8

# Add missing trailing slashes.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite to /pages
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /pages/$1 [QSA,L]
RewriteRule ^print/(.+)$ /$1?print=yes [QSA,L]

</IfModule>
Ulysses
 
Posts: 10
Joined: Thu Sep 18, 2008 7:58 am

Postby richardk » Fri Sep 19, 2008 8:17 am

Try
Code: Select all
AddDefaultCharset UTF-8
AddType application/x-httpd-php .html
Options +FollowSymlinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Add missing trailing slashes.
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI}/ -d
  RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

  RewriteRule ^print/(.+)$ /$1?print=yes [QSA,L]

  # Rewrite to /pages
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule ^(.*)$ /pages/$1 [QSA,L]
</IfModule>


Does it need to "Rewrite to /pages"? You might need to replace /$1?print=yes with /pages/$1?print=yes.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Ulysses » Fri Sep 19, 2008 8:23 am

Hi Richard,

Yes, I do need to rewrite to /pages as I'm omitting that folder from the URL.
Your code worked by the way ... I guess I wasn't too far off!?

Much appreciated, thanks.
Ulysses
 
Posts: 10
Joined: Thu Sep 18, 2008 7:58 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 26 guests

cron