Welcome to WebmasterWorld Guest from 54.162.141.212

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite help please...

i need advice on changing urls...

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

10+ Year Member



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

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

10+ Year Member



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

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

10+ Year Member



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!

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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!

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

10+ Year Member



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]

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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>
4:31 pm on May 19, 2003 (gmt 0)

10+ Year Member



YES! ITS ONLY SMEGGING WORKING!

Thanks a lot guys, its worked a treat!

Love you all! ;)

Thanks

Andy

 

Featured Threads

Hot Threads This Week

Hot Threads This Month