Mod_Rewrite

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Mod_Rewrite

Post 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!
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post by programmermatt »

You have it backwards, it is going to rewrite any request from 'domain.com/genre/whatever' to 'genre.php?g=whatever'
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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]
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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?
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

src="images/home.gif" :arrow: src="/images/home.gif"
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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 ?>"/>
(#10850)
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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?
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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.
Post Reply