Rewriting each directory as a php variable?

New to mod_rewrite? This is a good place to start.

Rewriting each directory as a php variable?

Postby zimbixelite » Thu Jun 18, 2009 12:23 am

Hiya,
I have been trying quite hard to get this to work, but I can't quite do it:
Basically, I want each subdirectory (up to three) of the root to be passed to the index.php page as a variable.

Here's a few examples:
    When a user browses to:
    http://www.mysite.net/games
    they are shown the contents of:
    http://www.mysite.net/?dir=games
    but the url shown in the address bar remains:
    http://www.mysite.net/games
    When a user browses to:
    http://www.mysite.net/apps/newapp/images
    they are shown the contents of:
    http://www.mysite.net/?dir=apps&subdir=newapp&subsubdir=images
    but the url shown in the address bar remains:
    http://www.mysite.net/apps/newapp/images


Here's my .htaccess file so far. The part in between the two "# TROUBLESOME" comments is the code that I have been trying to use to solve this problem. However, if you notice any problems/suggestions of improvement to other parts please let me know.

Any help is much appreciated! :D
Thankyou!
Code: Select all
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php


# TROUBLESOME
RewriteEngine On
Options +FollowSymLinks
Rewritecond $1 !^files/
Rewritecond $1 !^forum/
Rewritecond $1 !^ftp/
Rewritecond $1 !^ictest/
Rewritecond $1 !^joomla/
Rewritecond $1 !^lolol/
Rewritecond $1 !^mail/
Rewritecond $1 !^pages/
Rewritecond $1 !^pnxi/
Rewritecond $1 !^ralphi/
Rewritecond $1 !^rapidleech/
Rewritecond $1 !^school/
Rewritecond $1 !^style/
RewriteRule ^(.*)\/(.*)\/(.*)$ ?dir=$2&subdir=$3&subsubdir=$4 [R=301]
# END TROUBLESOME


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.net [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [R=301]


# REDIRECT /folder/index.htm to /folder/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
Rewritecond $1 !^ftp/
Rewritecond $1 !^rapidleech/
Rewritecond $1 !^joomla/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.mysite.net/$1 [R=301,L]
#

# REDIRECT /folder/index.html to /folder/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
Rewritecond $1 !^ftp/
Rewritecond $1 !^rapidleech/
Rewritecond $1 !^joomla/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.mysite.net/$1 [R=301,L]
#

# REDIRECT /folder/index.php to /folder/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
Rewritecond $1 !^ftp/
Rewritecond $1 !^rapidleech/
Rewritecond $1 !^joomla/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.mysite.net/$1 [R=301,L]
#
zimbixelite
 
Posts: 3
Joined: Wed Jun 17, 2009 10:43 pm
Location: Melbourne, Australia

Postby richardk » Thu Jun 18, 2009 11:32 am

Try
Code: Select all
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(html?|php)(\?.*)?\  [NC]
RewriteCond $1 !^(ftp|rapidleech|joomla)(/.*)?$
RewriteRule ^(.+/)?index\.(html?|php)$ http://www.example.net/%1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

RewriteCond $1 !^(files|forum|ftp|ictest|joomla|lolol|mail|pages|pnxi|ralphi|rapidleech|school|style)$
RewriteRule ^([^/]+)(?:/([^/]+(?:/([^/]+)?)?/?$ /?dir=$2&subdir=$3&subsubdir=$4 [QSA,L]


A few things:

You should always (nearly always, there are always exceptions) redirect first. You don't want to waste time processing rules if the next thing that happens is a redirect and the processing has to be done again.

Code: Select all
Rewritecond $1 !^files/

$1 will not end in a /, it would just be "files".

Code: Select all
RewriteRule ^(.*)\/(.*)\/(.*)$ ?dir=$2&subdir=$3&subsubdir=$4 [R=301]

You don't need to escape slashes.
There is not $4. You want $1, $2 and $3.
The R=301 flag means a visible redirect will happen, not an internal rewrite.

Code: Select all
^(.*)\/(.*)\/(.*)$

Will only match when there are at least 3 slashes (including the one after the domain).

Code: Select all
RewriteRule ^(.*)$ http://www.mysite.net/$1 [R=301]

Most rules should end in L (Last), especially redirects. If this rule matches, should any other rules be processed (THIS request, not after the redirect).

Code: Select all
# REDIRECT /folder/index.html to /folder/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
Rewritecond $1 !^ftp/
Rewritecond $1 !^rapidleech/
Rewritecond $1 !^joomla/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.mysite.net/$1 [R=301,L]

.html and .html.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

problem..

Postby zimbixelite » Thu Jun 18, 2009 9:03 pm

Hiya,
Thanks for the reply :D

I like the suggestions you made and have modified my code. However when I try to browse my site, the server replies with a code 500 error!

The code you gave me is a bit too complex for me to comprehend :-? but I noticed that after the first variable in the following line, there are four opening brackets and only two closing ones. Could this be the problem?
Code: Select all
RewriteRule ^([^/]+)(?:/([^/]+(?:/([^/]+)?)?/?$ /?dir=$1&subdir=$2&subsubdir=$3 [QSA,L]

(I changed the $2, $3 and $4 variables to fit with one of your other suggestions, I hope this is right)

Thanks for your help!
zimbixelite
 
Posts: 3
Joined: Wed Jun 17, 2009 10:43 pm
Location: Melbourne, Australia

Postby richardk » Fri Jun 19, 2009 10:22 am

The regular expression is wrong, try
Code: Select all
^([^/]+)(?:/([^/]+)(?:/([^/]+))?)?/?$

It uses (?:optional)? and nesting. The ?: makes the () not create a backreference/variable. http://www.regular-expressions.info/brackets.html.


(I changed the $2, $3 and $4 variables to fit with one of your other suggestions, I hope this is right)

I shouldn't copy and paste.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

still not working..

Postby zimbixelite » Sat Jun 20, 2009 8:36 pm

Hiya Richard,

I gave the edited version a go, and it still gives me a 500 error. I tried commenting out the other lines, and worked out that it is in fact still this line that's causing the trouble:
Code: Select all
RewriteRule ^([^/]+)(?:/([^/]+)(?:/([^/]+))?)?/?$ /?dir=$1&subdir=$2&subsubdir=$3 [QSA,L]

I have no idea why! It all looks good to me :-?

Thanks again for your help!
zimbixelite
 
Posts: 3
Joined: Wed Jun 17, 2009 10:43 pm
Location: Melbourne, Australia

Postby richardk » Sun Jun 21, 2009 10:26 am

Try adding
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$

before that rule.

I think the problem is that the request ends up as /index.php?dir... and therefore matches the rule and causes a loop.

You could also try updating the condition to add index.php
Code: Select all
RewriteCond $1 !^(files|forum|ftp|ictest|joomla|lolol|mail|pages|pnxi|ralphi|rapidleech|school|style|index\.php)$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 109 guests

cron