homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
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...

 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


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:


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!




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


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

Upload both the file into your root directory. I hope it will help you



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


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)


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]



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


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)


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


[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)


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.


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



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


Thanks a lot guys, its worked a treat!

Love you all! ;)



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