2

how can i auto redirect a site from dirty url to clean url in php , something like

http://www.mysite.com?page=page1&action=action1

to

http://www.mysite.com/page1/action1
Dipesh KC
  • 3,195
  • 3
  • 32
  • 50
  • I'd recommend you give an error for links like that to ensure canonical usage. It also helps to prevent wannabe hackers from trying to break your site! – Khez Apr 08 '11 at 13:06
  • Is this *really* what you want? Is it not the other way round? – Pekka Apr 08 '11 at 13:17
  • Thanks everybody. but all i wanted is to approach **clean url** mechanism in my webpage (for SEO ) [www.kcdipesh.com.np](http://www.kcdipesh.com.np/) And more important is that it is a single entry page so there is no folders to redirect my pages to. could you guys suggest me what is the best idea? – Dipesh KC Apr 11 '11 at 05:48

3 Answers3

5

You have to check if it was clean request or not. Otherwise you will fall into infinite loop

Here is an example from one of my projects:

.htaccess

RewriteEngine On
RewriteRule ^game/([0-9]+)/ /game.php?newid=$1

game.php

if (isset($_GET['id'])) {
  $row = dbgetrow("SELECT * FROM games WHERE id = %s",$_GET['id']);
  if ($row) {
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    Header( "Location: /game/".$row['id']."/".seo_title($row['name'])); 
  } else {
    Header( "HTTP/1.1 404 Not found" ); 
  }
  exit;
}

if (isset($_GET['newid'])) $_GET['id'] = $_GET['newid'];

So, you have to verify, if it was direct "dirty" call or rewritten one.
And then redirect only if former one.
You need some code to build clean url too.

And it is also very important to show 404 instead of redirect in case url is wrong.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
3

If you are running Apache you can use the mod_rewrite module and set the rules in a .htaccess file in your httpdocs folders or web root. I don't see any reason to invoke a PHP process to do redirection when lower level components will do the job far better.

An example from Simon Carletti:

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://mydomain.site/page/%1.pdf [R=302,L]
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • i think he is asking for a php solution – Fender Apr 08 '11 at 13:11
  • 1
    @Fender I don't see any reason to invoke a PHP process to do redirection when lower level components will do the job far better. – Treffynnon Apr 08 '11 at 13:12
  • thats right, i wouldn't do it with php either, but he mentioned it in his question. but your solution is good – Fender Apr 08 '11 at 13:13
  • I think it's not an answer at all. One can answer ANY question on SO this lame way – Your Common Sense Apr 08 '11 at 13:13
  • @Col. Shrapnel Using your recommendation I have just posted this as an answer to as many SO questions as I can! – Treffynnon Apr 08 '11 at 13:17
  • 1
    Unacceptable, no. However it wasn't particularly helpful I agree. – Treffynnon Apr 08 '11 at 13:32
  • But even if a contribution gets fixed, far be it from @Col. Shrapnel to actually revert a downvote. – Pekka Apr 08 '11 at 15:22
  • @Pekka I don't think anyone was asking for voting to change? I am not sure what you mean? – Treffynnon Apr 08 '11 at 15:26
  • @Treffynnon my understanding was that the answer was downvoted because it was incorrect, and subsequently edited to make it more correct. I think it goes without saying that one removes one's downvote if there is no longer anything incorrect in the post. This has nothing to do with you, just picking a bone with that user. Never mind. – Pekka Apr 08 '11 at 15:28
  • @Pekka I get the context now and see what you are driving at! :) – Treffynnon Apr 08 '11 at 15:32
2

try

header("Location: http://www.mysite.com/".$_GET['page']."/".$_GET['action']);

you should check whether the values are set before trying to redirect

Fender
  • 3,055
  • 1
  • 17
  • 25