jaymc Posted January 9, 2008 Share Posted January 9, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/ Share on other sites More sharing options...
madmax Posted January 9, 2008 Share Posted January 9, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-435045 Share on other sites More sharing options...
jaymc Posted January 10, 2008 Author Share Posted January 10, 2008 That cant be done gallery.site.com/jamie does not exist It has to be virtual, thus originally using mod rewrite I take it apache rewrite is the only way to perform this? Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-435498 Share on other sites More sharing options...
trq Posted January 10, 2008 Share Posted January 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-435501 Share on other sites More sharing options...
jaymc Posted January 10, 2008 Author Share Posted January 10, 2008 I have 800,000 access a day on the page that has mod rewrite.. thus even if effecient will cause problems with that much traffic? Im not sure, maybe Im am making a fuss over nothing Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-435524 Share on other sites More sharing options...
madmax Posted January 10, 2008 Share Posted January 10, 2008 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". Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-435692 Share on other sites More sharing options...
jaymc Posted January 10, 2008 Author Share Posted January 10, 2008 Cheers mate Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-435839 Share on other sites More sharing options...
trq Posted January 10, 2008 Share Posted January 10, 2008 As I said, mod_rewrite is built to do what you want to do and it is very efficient at it. Whats makes you think its not? Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-435914 Share on other sites More sharing options...
jaymc Posted January 11, 2008 Author Share Posted January 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-436330 Share on other sites More sharing options...
madmax Posted January 11, 2008 Share Posted January 11, 2008 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.) Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-436425 Share on other sites More sharing options...
jaymc Posted January 11, 2008 Author Share Posted January 11, 2008 Cheers for advice! Quote Link to comment https://forums.phpfreaks.com/topic/85203-solved-mod-rewrite/#findComment-436563 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.