Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1638 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

apache mod rewrite

Thread title: apache mod rewrite
Closed Thread    
    Thread tools Search this thread Display Modes  
10-16-2006, 02:44 PM
#1
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old  apache mod rewrite

i know a lot of people here were/are gung-ho on the whole clean-code/seo movement. whether you really believe in the mentality or your just reiterating what's already been said isn't important. people here know the tricks and i need your help.

i want to set up a site with "clean urls" instead of ?s and &s from peramiters. in other words, my end goal is to have the user type in/be linked to www.site.com/section/page and i could grab the peramiters as if it was served as www.site.com/index.php?sec=sections&page=page. i've been follwing a list apart article on it, and i understand the gist of breaking up the url at the / and getting the peamiters. the article i've been used for reference was http://www.alistapart.com/articles/succeed (notice the url)

the problem is, the htaccess file is being all screwy. when i try to access anything in the root folder, apache serves me a 500 internal server error - i found the error log and the error it is serving reads:


[Thu Oct 12 16:45:25 2006] [error] [client 127.0.0.1] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
here's where it gets messed up.

i've placed everything inside a folder called test (c:\wamp\www\test\). now, i know the mod_rewrite module is working, because i activated it in my httpd.conf, i turned it on in htaccess and when i used the following code, everything redirected successfully:

Code:
Options +FollowSymLinks

RewriteEngine On

# test for mod_rewrite, /google --> http://www.google.com/
RewriteRule ^google$ http://www.google.com/? [R,L]
i then told it to redirect everything to the main wamp index.php file in the www folder (instead of www/test/index.php where the htaccess is stored) and it redirected fine.

it also redirected fine when i told it to redirect to another site's folder (www/springlake/index.php). it seems to only breaks when the redirection points to the same folder that .htaccess is in (www/test/index.php), which is the one i need it to point to. why it gets stuck in that infinate loop i don't know.

any help will be appreciated, i'm sure i can return the favour somehow.

10-16-2006, 02:47 PM
#2
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Copy the .htaccess which causes the infinite loop into a post.

10-16-2006, 04:28 PM
#3
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old

it used to read
Code:
RewriteEngine on
RewriteRule !\.(gif|jpg|png|css)$ /test/index.php
however, i did some more poking around today and i found the mod_rewrite cheat sheet. after some dissection, i was able to get the rewrite working properly on both localhost and the shared web server.

the new code looks like this:
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/index.php

# domain.com/section/page/etc to domain.com/index.php?sec=section&page=page&etc=etc
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?sec=$1&page=$2&etc=$3 [L]

</IfModule>
i'm still not sure why the first snip would create an infinate loop. if i read it correctly, it was redirecting all requests inside the test folder to infex.php unless they ended in those specific extensions.

10-16-2006, 06:04 PM
#4
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Well, it seems you've got a working solution so there's no need for a 'correction'. However, I can explain why an infinite loop was happening in the first snippet.

The RewriteRule is saying, "if the requested file doesn't end in .gif, ... .css then go to /test/index.php". However, when the request is made to index.php it doesn't end in .gif, ... .css and so redirects another time to /test/index.php. And because index.php doesn't end in.... You get the idea.

A method that I use is slighly different and may or may not be more beneficial for you would be something along the lines of:
PHP Code:
# Rewriting requests for pretty URLs
RewriteEngine On

# Allowed request are from 1 to 3 levels deep:
# domain.com/section
# domain.com/section/page
# domain.com/section/page/cruft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteCond 
%{REQUEST_URI} ^/rewrite(/[a-zA-Z0-9_\-]+){1,3}/?$
RewriteRule ^([^/]+)/?$ index.php?sec=$[QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?sec=$1&page=$[QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?sec=$1&page=$2&etc=$[QSA,L
A little more confusing, perhaps, but it should be a good starting (or ending!) point for you.

10-16-2006, 06:50 PM
#5
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old

i also ran into the common image path problem.

Code:
# rewrite image paths
RewriteRule ^[a-z0-9]+/images/(.+)$ /images/$1 [L]
RewriteRule ^[a-z0-9]+/[a-z0-9]+/images/(.+)$ /images/$1 [L]
that needs to be added to the htaccess in order to get image paths to work. you're probably going to have to do the same for css and js files, so i suggest storing them in a global include folder so you can rewrite all the urls to point to it instead of trying to work around a call to the root folder and having it bypass the rewrie to index.php. again, you'll have to write a rule for every directory level you use.

i hope that made sense.

edit: i see what the error is now salathe. that article has turned out to be very poorly written. i got into php when php5 came out, so i don't know how well it would have worked with php4, but i had to change to much to try and get all the $_SERVER['REQUEST_URI'] stuff to work that it just didn't seem worth it.

in the end i just wanted to be able to manipulate the page like i normally would using the ? and &. thanks for your help, your htacces code is pretty close to what i'm using now.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed