Jump to content

[SOLVED] Mod rewrite


jaymc

Recommended Posts

Just a quick one

 

I want to turn my links from

 

www.site.com/index.php?gallery=jamie

to

gallery.site.com/jamie

 

I can do that fine but from my understanding its quite slow in regards to server performance, as in, you have a lot of pages being accessed that use the mod rewrite then as a result performance will struggle

 

Asuming worst case scenario, is there a better way to to perform this little trick which is more efficient?

 

Cheers!

Link to comment
Share on other sites

PHP redirection would be a better bet for redirection if you're really concerned about the rules taking up CPU time (although the rules are pretty efficient). This example is fairly primitive on error-checking but at least it only takes CPU time when the index.php file is actually called. You would need to add index.php to your range of registered index files to ensure it was always called even if no explicit file specified.

 

DirectoryIndex index.htm index.html index.php

 

<?
//INDEX.PHP - Redirect http://www.site.com/index.php?gallery=jamie
$relativeScriptPath = $_SERVER["PHP_SELF"];
$requesturi = $_SERVER["REQUEST_URI"];

$gallery = $_GET['gallery'];

$self='http://'.$_SERVER['HTTP_HOST']; /* Build up full HTTP:// URL */
$relativeScriptPath=substr($relativeScriptPath,0,strrpos($relativeScriptPath,"/"));

//Debug (next 2 lines only)
//ECHO "Redirecting to $self$relativeScriptPath/$gallery";
//die();

//Redirect to the correct location according to the request variable
if($gallery!="")
  header("Location: $self$relativeScriptPath/$gallery/");
else
  echo "No parameter was given - no redirection performed";
exit;

?>

Link to comment
Share on other sites

I can do that fine but from my understanding its quite slow in regards to server performance

 

Where did you hear that? Mod_rewrite is designed specifally to do what you want (and much much more) and is very efficient at doing so.

Link to comment
Share on other sites

Still not sure what you're trying to achieve here. My guess is that you want to "fool" the visitor by making them see something different in their browser URL bar. Perhaps for reasons of tidiness although I can't quite see the point. Unfortunately that isn't how the client-server mechanism works. The browser won't readjust unless you redirect to a new URL since HTTP is a stateless transaction and it knows nothing of what your Apache server is doing behind the scenes until the sever sends back a response.

 

What you could try using javascript to rewrite the "apparent" URL the client sees in their browser software. However this would be complex and you'd need to deal with differing browsers and different quirks. For text-mode browsers you'd be stumped though. I don't know of any which support javascript.

 

I'd have thought that the best way of achieving this would be to design your physical or virtual directory map to work with the rewrite rule suggested from the "get go".

 

 

Link to comment
Share on other sites

A pretty knowledgeable guy told me he disabled htaccess on one of his live servers which is a production one because it adds to load

 

Perhaps I misunderstood..

 

I jsut thought that with that rule taking place every single time the page is displayed, if the page is accessed a lot, problems may pop up

 

But suppose its cool

Link to comment
Share on other sites

mod_rewrite is pretty efficient and, under normal circumstances with well-constructed rulesets it shouldn't cause too many headaches with adequate hardware.

 

However the impact, as you point out, would be scalar according to traffic and dependent on your kit.  Many people posting on forums are running home-brewed webservers (mine is a hobbyist Pentium 800) so issues of parsimony would be of some relevance to those reading this thread.

 

Therefore, it's always worth thinking a little bit about problems and solutions since if you can find a quick, easy and efficient solution then you will save a small but finite amount of CPU time. In other words mod_rewrite is funky but if there's a simple solution which only has an overhead when the page is actually accessed I'd tend to go for that myself. PHP is pretty useful here and although I'm not keen on Javascript myself it does have it's place.

 

Like some supermarket chain says - "every little helps" ;)

 

(Usual caveats - IMHO etc.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.