Forum Moderators: phranque
I'm new to the forum so go easy on me ;)
I've red through loads of posts but couldnt find something that quite answered my question and i can't really find a quality tutorial anywhere that helps either *so* I'm hoping you guys can help me...
Basically I have a website on an apache server that has dynamically generated pages in the format of
[mydomain.co.uk...]
What i'd like to do is make these url's search engine freindly, but also a lot nicer to look at so they appear as:
htp://www.mydomain.com/articles/article_2.html
I know that i need to use mod-rewrite in a .htaccess file (as i dont have root access to change httpd.conf) but this is beyond my scope of expertise.
Is their anyone out there that could help me?
Thanks for your time!
Andy
Try this
/////Save this file as article (without any extension)
<?
$dirname = getenv("REQUEST_URI");
if (substr($dirname,-1,1)!= "/") {
$dirname = $dirname."/";
}
$dirname = split("/", $dirname);
$region = $dirname[sizeof($dirname) - 2];
if ($region == "article") {
header("Location: /");
}
?>
///// Save this file as .htaccess file
<Files article>
ForceType application/x-httpd-php
</Files>
Upload both the file into your root directory. I hope it will help you
Navin
Welcome to WebmasterWorld [webmasterworld.com]!
Basically, what you'll need to do is to modify your scripts to generate the search-engine-friendly URLs on all pages which your visitors see. Then you can use mod_rewrite to convert those friendly URLs back to the URLs required by your dynamic page-generation scripts.
Mod_rewrite works in the API phase where a request is being processed and before a page is served in response to that request. Therefore, it cannot change the "output URLs" from your scripts and pages, it can only change the URLs requested by the user's browser.
You will need to rewrite the requested friendly URL htp://www.mydomain.com/articles/article_2.html to [mydomain.co.uk...]
Therefore, the mod_rewrite code should be something like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^articles/article\_([0-9]{1,4})\.html$ /articles_view.php?articleid=$1 [L]
Ref: Introduction to mod_rewrite [webmasterworld.com]
HTH,
Jim
I've asked my freind who owns the server nicely and he's given me root access.
The rewrite rules in httpd.conf currently look like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^212.84.175.XXX(:80)?$
RewriteCond %{HTTP_HOST} !^www.mydomain.co.uk(:80)?$
RewriteRule ^/(.*) [mydomain.co.uk...] [L,R]
RewriteOptions inherit
I don't know if thats any help, but at least i can now change the rewrite rules without using htaccess...
Thanks for the continued support
Andy
[edited by: DaveAtIFG at 2:51 pm (utc) on May 19, 2003]
[edit reason] URL revised [/edit]
You should be able to use the RewriteRule posted above in httpd.conf by adding a slash after the "^", making it "RewriteRule ^/articles...
You might ask your friend to add "AllowOverride All" to your priveleges. This would allow you to experiment with mod_rewrite in .htaccess, where an error will only affect your account, and not the others on the machine. In addition, you won't have to restart Apache each time you make a change to your rules as you will in httpd.conf.
HTH,
Jim
<added>In order to avoid problems with search engine listings, you really should change your current RewriteRule in httpd.conf to:
RewriteRule ^/(.*)$ http://www.mydomain.co.uk/$1 [L,[b]R=301[/b]]