Category Page 301 Redirect

Discuss practical ways rearrange URLs using mod_rewrite.

header use

Postby pushpinderbagga » Fri Jul 17, 2009 3:36 pm

can we use the below code in the story.php ?

Code: Select all
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

please help

Postby pushpinderbagga » Sat Jul 18, 2009 6:07 am

hi richard...

anything ?

moreover ... could we please remove my threads as I have exposed my whole htaccess here which should not have been dont ... it displays the whole site structure...
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Sat Jul 18, 2009 8:54 am

I have access to the file... dont know where it is...

What operating system are you using?

This means you could use a RewriteMap. It could either be a text file (that you could craete with a PHP script), eg.
Code: Select all
# story_id story_url
1 /CATEGORY/1/TITLE-HERE.HTML
2 /CAT2/1/TITLE.HTML

Or you could use a program, for example a PHP script.

The RewriteMap definition would need to be in the httpd.conf (or similar .conf file) file, possibly this domain's <VirtualHost>. For a text file
Code: Select all
RewriteMap storymap txt:/path/to/story.map

or for a PHP script
Code: Select all
RewriteMap storymap prg:/path/to/story.map.program


Then you could do (in the .htaccess file)
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?id=([0-9]+)(&.*)?$ [NC]
# Look it up in the storymap
RewriteCond ${storymap:%2|failed} ^(.+)$
# Make sure a URL was returned.
RewriteCond %1 !^failed$
RewriteRule ^story\.php$ %1 [R=301,L]


can we use the below code in the story.php ?
Code: Select all
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

yes, or
Code: Select all
header('Location: http://www.example.com/', true, 301);


You would want to put it in an IF such as
Code: Select all
<?php

if(getenv('REDIRECT_STATUS') === false && isset($_GET['id']) && is_numeric($_GET['id']))
{
  $id = $_GET['id'];

  # Search for the category and title in the database.
  $category = ...;
  $title = ...;

  # Redirect.
  header('Location: http://www.example.com/' . $category . '/' . $id . '/' . $title . '.html', true, 301);
  exit();
}


anything ?

Patience?

moreover ... could we please remove my threads as I have exposed my whole htaccess here which should not have been dont ... it displays the whole site structure...

This forum is for learning, i will not delete the thread. I can replace your domain and (some of) the variable names if you want.

What is the problem with exposing your .htaccess file? Surely all the old URLs are already know or you wouldn't need the redirects.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

will try

Postby pushpinderbagga » Sat Jul 18, 2009 6:13 pm

Hi Richardk

I appreciate... I was just in a hurry as there was traffic loss... :D

I am using windows vista ultimate... but I access my hostgator account via dreamweaver ftp ...

so you mean that if its a text file...

its an automated php script editing a text file everytime a story is published and adding its category and title to it... that could be done but with a little study over this...

if we use the below code...

Code: Select all
<?php

if(getenv('REDIRECT_STATUS') === false && isset($_GET['id']) && is_numeric($_GET['id']))
{
  $id = $_GET['id'];

  # Search for the category and title in the database.
  $category = ...;
  $title = ...;

  # Redirect.
  header('Location: http://www.example.com/' . $category . '/' . $id . '/' . $title . '.html', true, 301);
  exit();
}


genenv checks if its a story.php or a mod_rewritten url ???

or we could also check if the document location contains a story.php string

yes I agree and we could replace all okidda with MY-DOMAIN thing... thanks...

lemme try the above loop and get back...
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

not working

Postby pushpinderbagga » Sat Jul 18, 2009 6:26 pm

richardk... the above loops isnt working ... :(

I echoed the redirect status... it comes to 200 for both URLS

Code: Select all
http://www.okidda.com/story.php?id=1403


and

Code: Select all
http://www.okidda.com/opinion/1403/pakistani-ministers-and-pakistani-people-ministers.html
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

tried the same

Postby pushpinderbagga » Sat Jul 18, 2009 7:05 pm

hi richardk

I tried the below code loop instead... it worked... but I dont know if its the correct way of doing it...

Code: Select all
if(strpos($_SERVER['REQUEST_URI'], "story.php") !== false && isset($_GET['id']) && is_numeric($_GET['id']))
{
  $idRR = trim($_GET['id']);
  # Search for the category and title in the database.
  $categoryRR = trim(strtolower($row['category']));
  $titleRR = createLink(trim(htmlentities($row['title'])));

  # Redirect.
  header('Location: http://www.okidda.com/' . $categoryRR . '/' . $idRR . '/' . $titleRR . '.html', true, 301);
  exit();
}
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Sun Jul 19, 2009 5:29 pm

the above loops isnt working

It's not a loop, it's an IF statement.

I tried the below code loop instead... it worked... but I dont know if its the correct way of doing it...

It looks fine to me.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Previous

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 13 guests

cron