rewrite with querystring and smurfID

Using mod_rewrite to handle various content issues

rewrite with querystring and smurfID

Postby Kjetil » Fri Oct 12, 2007 5:27 am

Hi! I have this type of URL that I would like to set as search engine friendly link and rather have the server rewrite it:

.../myprog?TASK=cart&c=y&smurfid=12675411005520071012027182756616

as you see it's quite long with that session- or smurf-id.

.../cart/y/12675411005520071012027182756616

- what would the rewrite rule be for this one?
- maybe I will need to have the smurfid left out of there or isn't it really a problem?
(my site uses smurfs insstead of cookies, that's why I need it there)

thanks for any help
Kjetil
 
Posts: 5
Joined: Fri Oct 12, 2007 3:20 am

Postby richardk » Fri Oct 12, 2007 11:04 am

Does cart change? Does y change? Will it always be three variables?

I would leave the session ID as a query string variable as it's not really part of the URL (it's unique to the visitor). For /cart/y/?smurfid=12675411005520071012027182756616 to /myprog?TASK=cart&c=y&smurfid=12675411005520071012027182756616, put the following in a .htaccess file in the same directory as myprog
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ ./myprog?TASK=$1&c=$2 [QSA,L]


For /cart/y/12675411005520071012027182756616 to /myprog?TASK=cart&c=y&smurfid=12675411005520071012027182756616, put the following in a .htaccess file in the same directory as myprog
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ ./myprog?TASK=$1&c=$2&smurfid=$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Kjetil » Mon Oct 15, 2007 12:06 am

Hi, thanks for quick reply. Actually I will need to use two types of rewrites, one for the list(s) of items, an one for the item details page(s).

the URL will always start with this type of string:
http://www.mysite.com:myport/myprog?

I have three basic page types that i need rewrites for:

1 - the item detail url:
...TASK=itemdetail&itemno=56997&smurfid=12675409385920071015165836427390

2 - the item list url:
...page=62&smurfid=12675409385920071015165836427390

3 - info page:
...task=Info&smurfid=12675409385920071015165836427390

So the code would look something like this for the item detail, I guess?

Code: Select all
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([itemdetail/]+)/([^/]+)/([^/]+)/?$ ./
myprog?TASK=$1&itemno=$2&smurfid=$3 [QSA,L]


For all other links it's not important to levae them search engine friendly, but then again they could be included into the same rules, if only for keeping linking the same way all over.

However, can I include these different kind of rules into the same htacces or config file - that I'll have to do with rewritecond in some way, right, but how?

I'd be very grateful for help on this, guess it'll save me from hours of reading and searching on the topic!! - You've got a really helpful place here :)

PS I have access to the apache config file and plan to put the rewrite rules there, I guess that will work just as well as in a htaccess file - besides the site is totally dynamic, so the html files don't exist actually. :)
Code: Select all
Kjetil
 
Posts: 5
Joined: Fri Oct 12, 2007 3:20 am

Postby richardk » Tue Oct 16, 2007 7:47 am

For
Code: Select all
1 - the item detail url:
/itemdetail/56997/12675409385920071015165836427390/ instead of /myprog?TASK=itemdetail&itemno=56997&smurfid=12675409385920071015165836427390

2 - the item list url:
/page67/12675409385920071015165836427390/ instead of /myprog?page=62&smurfid=12675409385920071015165836427390

3 - info page:
/info/12675409385920071015165836427390/ instead of /myprog?task=Info&smurfid=12675409385920071015165836427390

Put the following in your domain's <VirtualHost>
Code: Select all
  Options +FollowSymLinks

  RewriteEngine On

  RewriteRule ^/itemdetail/([0-9]+)/([0-9]+)/?$ /myprog?TASK=itemdetail&itemno=$1&smurfid=$2 [QSA,L]
  RewriteRule ^/page([0-9]+)/?$                 /myprog?page=62&smurfid=$1                   [QSA,L]
  RewriteRule ^/info/([0-9]+)/?$                /myprog?TASK=Info&smurfid=$1                 [QSA,L]


For all other links it's not important to levae them search engine friendly

If you leave the smirfid as part of the URL (not a cookie or in the query string) you will probably get penalised for duplicate pages. I suggest you leave it as a query string parameter and even make it optional to be in a cookie (like PHP sessions work).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Kjetil » Wed Oct 17, 2007 12:36 am

ok, I think I start to get the picture here now, about the rewriting anyway...

However, the apache server config I run is without any virtual host directory. So, I put the rewrite rule somewhere quite at the top.

Now, I get 403 forbidden message - it's as if the rewrites were not applied and the server is looking for that physical path - which doesn't acually exist, and would also be forbidden :S

Sorry for being a real amateur or the worse on apache config :( you're being of great help though.I'll be waiting for your reply.
Kjetil
 
Posts: 5
Joined: Fri Oct 12, 2007 3:20 am

Postby richardk » Wed Oct 17, 2007 12:26 pm

Try this mod_rewrite test (it will work in your httpd.conf file).

Try replacing all
Code: Select all
[QSA,L]

with
Code: Select all
[PT,QSA,L]


If that doesn't work, try adding
Code: Select all
%{DOCUMENT_ROOT}

before
Code: Select all
/myprog

(still with the PT flag).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Kjetil » Thu Oct 18, 2007 3:26 am

thanks again. You know, that PT stuff didn't work, but now the rewrite functions as written in your last reply!?! don't know what happened on the server overnight, but today this functions (which it did NOT yesterday)...

I'll experiment a bit with the structure etc for this rule-set now, and guess I'll come back to you at the next rewrite problem - when or if it occurs.

Thanks a lot, you've been of great help!
Kjetil
 
Posts: 5
Joined: Fri Oct 12, 2007 3:20 am

Postby Kjetil » Mon Oct 22, 2007 12:36 am

trouble: SMURFIDs causing 404 not found.

the smurf id's are created by the server to be unique for each browser session. so on linking to a dynamical "page", the smurf id is normally not there when you arrive there first time. i.e. if I send you a link to my page including the smurf id and then close my browser, you will still get 404 because the smurf id does not exist anymore.

so, can I make that parameter OPTIONAL in the rewrite?

PS as long as I enter through the home page using the address .../myprog all is ok, cause I will be assigned MY smurf id, but any address including that smurf id will not be valid after I quit the session.
Kjetil
 
Posts: 5
Joined: Fri Oct 12, 2007 3:20 am

Postby richardk » Mon Oct 22, 2007 3:32 pm

This is why they shouldn't be part of the file/directory name (they should be in a cookie and if cookies are not accepted the query string). This will allow for no smurf ID but will not get rid of invalid IDs.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^/itemdetail/([0-9]+)(/([0-9]+))?/?$ /myprog?TASK=itemdetail&itemno=$1&smurfid=$3 [QSA,L]
RewriteRule ^/page([0-9]+)(/([0-9]+))?/?$        /myprog?page=$1&smurfid=$3                   [QSA,L]
RewriteRule ^/info(/([0-9]+))?/?$                /myprog?TASK=Info&smurfid=$2                 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Content

Who is online

Users browsing this forum: No registered users and 9 guests

cron