homepage Welcome to WebmasterWorld Guest from 54.204.127.56
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite help please...
i need advice on changing urls...
andy206uk




msg:1522809
 1:02 pm on May 19, 2003 (gmt 0)

Hiya guys,

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

 

navink




msg:1522810
 1:19 pm on May 19, 2003 (gmt 0)

Hi

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

andy206uk




msg:1522811
 1:42 pm on May 19, 2003 (gmt 0)

Hi...

It doesnt appear to do anything...

I uploaded the file to my web root and uploaded the htaccess file and now none of my pages work!

Any ideas what went wrong?

Surely theres a way to do this using mod rewrite?

Thanks for your help so far!

jdMorgan




msg:1522812
 2:08 pm on May 19, 2003 (gmt 0)

andy206uk,

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]

As shown, the rule will accept a 1-to-4-digit article number; you can change that by modifying the contents of the curly brackets from {1,4} to whatever you need.

Ref: Introduction to mod_rewrite [webmasterworld.com]

HTH,
Jim

andy206uk




msg:1522813
 2:14 pm on May 19, 2003 (gmt 0)

Hi,

I don't know why but whenever i add a .htaccess file with rewrite rules in it, all my pages stop working... i'm confused, is there something not right on the server?

Thanks for your help!

andy206uk




msg:1522814
 2:27 pm on May 19, 2003 (gmt 0)

Hiya,

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]

jdMorgan




msg:1522815
 3:04 pm on May 19, 2003 (gmt 0)

andy206uk,

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]]

</added>

andy206uk




msg:1522816
 4:31 pm on May 19, 2003 (gmt 0)

YES! ITS ONLY SMEGGING WORKING!

Thanks a lot guys, its worked a treat!

Love you all! ;)

Thanks

Andy

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved