Page 1 of 2

Mod_Rewrite

Posted: Sat Apr 22, 2006 3:30 pm
by GeXus
Hello, I made the following code to put in my htaccess

Code: Select all

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^genre/(.+) genre.php?g=$1 [r=301,nc]
Now, from what I can tell, this should work.... (I think).. Basically it would have any request to genre.php?g=whatever use domain.com/genre/whatever instead....

Well, it's not working.. Any help would be great!

Posted: Sat Apr 22, 2006 5:20 pm
by programmermatt
You have it backwards, it is going to rewrite any request from 'domain.com/genre/whatever' to 'genre.php?g=whatever'

Posted: Sat Apr 22, 2006 6:35 pm
by GeXus
Is this what I would want? This doesnt seem to work either..

Code: Select all

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^genre.php?g=(.+) dvds/genre/$1 [NC]

Posted: Sat Apr 22, 2006 8:13 pm
by John Cartwright
programmermatt wrote:You have it backwards, it is going to rewrite any request from 'domain.com/genre/whatever' to 'genre.php?g=whatever'
It actually was right the first time ;)

How about

Code: Select all

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/genre/(.+)$ genre.php?g=$1 [NC]

Posted: Tue May 02, 2006 2:45 am
by rsmarsha
Just found this post. I am having rewrite problems too.

I found this in a tutorial:

RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ ad_index.php?page=$1

That should rewrite page/orders to index.php?page=orders

When i enter in

url/page/orders

i just get a 404 error. :(

Any ideas what i'm doing wrong?

I also tried

RewriteRule ^home\.php home2.php

to take home.php and redirect to home2.php and i get another 404 error.

Also found another tutorial at : http://www.sitepoint.com/article/guide-url-rewriting/2

so tried this :

RewriteRule index/([a-z]+) index.php?page=$1

so when i typed in url/index/orders the window should show me index.php?page=orders , and again i get the 404 error.

I've read a few tutorials and tried their basic rewrites, so i must be missing something. :(

If anyone could help me figure out what i'm missing that would be great.

Posted: Tue May 02, 2006 5:26 am
by Chris Corbyn
rsmarsha wrote:Just found this post. I am having rewrite problems too.

I found this in a tutorial:

RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ ad_index.php?page=$1

That should rewrite page/orders to index.php?page=orders

When i enter in

url/page/orders

i just get a 404 error. :(

Any ideas what i'm doing wrong?

I also tried

RewriteRule ^home\.php home2.php

to take home.php and redirect to home2.php and i get another 404 error.

Also found another tutorial at : http://www.sitepoint.com/article/guide-url-rewriting/2

so tried this :

RewriteRule index/([a-z]+) index.php?page=$1

so when i typed in url/index/orders the window should show me index.php?page=orders , and again i get the 404 error.

I've read a few tutorials and tried their basic rewrites, so i must be missing something. :(

If anyone could help me figure out what i'm missing that would be great.
Maybe one of the following is causing the issue:

1. The server is not Apache (i.e. it might be IIS)
2. Apache is not configured to read .htaccess files
3. mod_rewrite is not compiled into apache

Posted: Wed May 03, 2006 2:45 am
by rsmarsha
I was told it is, will check again. :) Can also test on my own server.

If i use one that changes

index.php?page=name

into

index/name

It seems to work, but it's not using the stylesheet and the images don't show correctly. Is this normal?

Posted: Wed May 03, 2006 3:09 am
by phpdevuk
most of the time I use rewrite rules in the following format

RewriteRule ^home/ home.php [L]

any request to http://somesite.com/home/ goes to home.php and anything appended to the URL gets sent as normal variables, dunno if that helps anyone or not.

Posted: Wed May 03, 2006 3:10 am
by Chris Corbyn
rsmarsha wrote:I was told it is, will check again. :) Can also test on my own server.

If i use one that changes

index.php?page=name

into

index/name

It seems to work, but it's not using the stylesheet and the images don't show correctly. Is this normal?
Just run phpinfo() and you should be able to see what's there.

Posted: Wed May 03, 2006 3:22 am
by rsmarsha
Here is the one i tested on my own server (simple test on a portal sites index)

http://www.irealms.co.uk/index/links

Should link to

http://www.irealms.co.uk/index.php?page=links

If you try the 1st url you'll see what i mean about it now showing correctly when compared to the other.

The .htaccess is as follows:

Code: Select all

RewriteEngine on
RewriteRule ^index/([a-z]+) index.php?page=$1 [L]
There is some other info in that file which was already there (default from shared host).


The php info for the other server i'm running it on (the one where i get nothing), is Here

Posted: Wed May 03, 2006 5:48 am
by rsmarsha
The one on my shared server does seem to work, but images and css still not working as they should when using the rewritten links.

This mod_rewrite thing is a bit confusing atm. :)

Posted: Wed May 03, 2006 8:00 am
by John Cartwright
src="images/home.gif" :arrow: src="/images/home.gif"

Posted: Wed May 03, 2006 11:48 am
by Christopher
rsmarsha wrote:The one on my shared server does seem to work, but images and css still not working as they should when using the rewritten links.

This mod_rewrite thing is a bit confusing atm. :)
When rewriting using clean URLs I have not found a better solution than to add a <base href="/base/path/"/> to the header. It makes images, ces and links all work properly. I started a thread about it here but have not found any better methods to deal with it.

I usually have it in the main/outer template, something like:

Code: Select all

<html>
<head>
<base href="{BASE}"/>

// or 

<base href="<?php echo $config->base ?>"/>

Posted: Thu May 04, 2006 2:24 am
by rsmarsha
Thanks both. :)

Will give that a go.

Now for the other server i work on, is it me or does the php info on that have no mention of apache at all? They told me mod_rewrite was installed but i can't find any mention of it or apache.

*edit*

Added http://www.irealms.co.uk as the base ref and it works fine.

Change the links to the format index/page but the link (index.php?page=eq2server), doesn't work when i change to the new format, no idea why.

Also i tried the rule :

RewriteRule ^([a-z]+) index.php?page=$1 [L]

as opposed to :

RewriteRule ^index/([a-z]+) index.php?page=$1 [L]

and that didn't work, is there a reason why?

Posted: Fri May 05, 2006 3:18 am
by rsmarsha
Fixed the eq2server link, hehe, i didn't have 0-9 in the rule. :oops:

Still can't get the oneandone server to work with the rules, and there is no mention of apache in the phpinfo results.