View Full Version : fun with Mod Rewrite
sprouty
07-21-2006, 06:15 PM
Hi,
I've been playing with this mod rewrite thingy today, I was expecting it to be a real pain but it turned out to be quite fun.
I've managed to reduce 30-odd .php file to three which should make for some speedy spring cleaning (I'm all for that).
Mostly (uniquely) I've used it tom make www.domain.tld/file.php?variable=value look like www.domain.tld/value.html. Which is pretty cool for a beginner like me.
But I want to do simpler things and I can't find any guidence for simplicity in the pool we call Google.
I'd like to simply change www.domain.tld/file.php to www.domain.tld/file.html
Sounds silly, right? I should be able to figure it out given the code I'm using. But I can't.
Can anybody help with this mess? Must I purchase (yet) another book?
The only reason I want to do it is for the sake of standardizing - it looks messy, to me, to have a site with a majority of .html pages but a few .php guys hitching a ride.
Any ideas? ANybody? Please?
:hurt:
rewriteRule ^index\.html$ /index\.php
enjoy :D
Yes, but I guess that would only work if you use php for ALL your pages.
Of course you could use this rule and ALSO rename all your existing .html pages to .php
Alternatively, you could name all your pages [whatever].html and use .htaccess to make your server parse them as php code.
Something like:
AddType application/x-httpd-php .php .htm .html
There could be more code involved but this is a start...
sprouty
07-24-2006, 05:54 PM
Thanks Gabs and thanks Witie - great advice - I was actually looking for a Gabs kind of reply as the pages I want to show as html don't actually exist, but Witie's advice will come in handy ;)
It turned out, in the end, that I probably won't need to do that anyway because the site I'm changing mostly requires multiple pages per file (if you see what I mean).
The only file that doesn't need to change dynamically (much) is the index page and I think search engines will not worry too much about that as long as the index file isn't directly referenced?
I mean: the index page is a .php file, but is always referenced as http://www.domain.tld/ and never as http://www.domain.tld/index.php - so why change it into a .html, right? (or wrong???)
in anycase, this mod rewrite thing is a babe - mix it up with mysql and I'm seeing some major housework advantages already :)
I love learning...
:peep:
I mean: the index page is a .php file, but is always referenced as http://www.domain.tld/ and never as http://www.domain.tld/index.php - so why change it into a .html, right? (or wrong???)
I take it your using php...
The above is write but sometimes a link to index.php / index.html can slip in.. Or even a competitor can link to it effecting your serps till G etc.. work it out..
It worth adding this at the very top of your index page..
<?php
$site_base_url = 'http://www.domain.tld';
function assertProperURL($site_base_url, $proper_url)
{
$request_uri = $_SERVER['REQUEST_URI'];
$current_url = $site_base_url . $request_uri;
if ($current_url != $proper_url) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $proper_url);
}
}
assertProperURL($site_base_url, $site_base_url . '/');
?>
This 301 redircts to www.domain.tld from index.php removing all chances of dup content on that page...
Hope that helps :D
sprouty
07-26-2006, 04:41 PM
Thanks Gabs!
That looks pretty neat - I'll need to play with it and see how it goes.
By the way, what do the two extra parameters to assertProperURL() do : '/' and true ?
assertProperURL($site_base_url, $site_base_url . '/', true);
Thanks
I take it your using php...
The above is write but sometimes a link to index.php / index.html can slip in.. Or even a competitor can link to it effecting your serps till G etc.. work it out..
It worth adding this at the very top of your index page..
<?php
$site_base_url = 'http://www.domain.tld';
function assertProperURL($site_base_url, $proper_url)
{
$request_uri = $_SERVER['REQUEST_URI'];
$current_url = $site_base_url . $request_uri;
if ($current_url != $proper_url) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: ' . $proper_url);
}
}
assertProperURL($site_base_url, $site_base_url . '/', true);
?>
This 301 redircts to www.domain.tld from index.php removing all chances of dup content on that page...
Hope that helps :D
Thanks Gabs!
That looks pretty neat - I'll need to play with it and see how it goes.
By the way, what do the two extra parameters to assertProperURL() do : '/' and true ?
Thanks
Oky.. The '/' isn't a param see the . ?
This adds a / to the end of the url.. you can of course remove it.. up to you..
The true param, well that would be telling (forgot to remove) ;)
sprouty
07-28-2006, 04:34 PM
Oky.. The '/' isn't a param see the . ?
This adds a / to the end of the url.. you can of course remove it.. up to you..
The true param, well that would be telling (forgot to remove) ;)
Aha - thanks! I read through this a few times before the penny dropped - I didn't realise you could use the '.' in function parameters!
That's great.
And as for the TRUE - well, you've posted it now, you're obloged to explain - sorry, it's not me - it's internet forum etiquette ;)
And as for the TRUE - well, you've posted it now, you're obloged to explain - sorry, it's not me - it's internet forum etiquette ;)
What "true" ?????????
:D
sprouty
07-31-2006, 04:25 PM
What "true" ?????????
:D
lol!
No, not "true" but "TRUE" ;)
You can only redeem your self esteme if you explain yourself - it's written in blood - you quoted a parameter that you now need to explain - everyone knows that! ;)
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.