Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite conditional

mod_rewrite conditional

         

jahndi

2:14 am on Mar 6, 2005 (gmt 0)

10+ Year Member



I need help bad. I have two directories, one being the front-end area, the other being the admin. So far, my rewriting for the main area has worked fine:

Options +FollowSymLinks
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/
RewriteRule ^([a-zA-Z0-9_]+)\/?([a-zA-Z0-9_/]+)$ /user/index.php?page=$1&args=$2

This doesn't work for mysite.com/admin because of that rewrite. I got another rewrite to work with /admin/ in there, but for some reason, my [L] tag won't work and it still processes the last rewrite. What I need is for it to do this:

mysite.com/anything_but_admin/args to redirect as I have above, and mysite.com/admin/args to redirect to either skip that last rewrite rule or apply a different one.

I've racked my brain for days and I just can't seem to find the documentation to teach me the workarounds here. Any help would be greatly greatly appreciated. Thanks

jdMorgan

4:23 am on Mar 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jahndi,

Welcome to WebmasterWorld!

I'd suggest adding two instances of RewriteCond [httpd.apache.org] to prevent rewrites when accessing /admin/ and to prevent an infinite loop on /user/index.php.

If you place mod_rewrite code in .htaccess, it will essentially act as if it is recursive, because the output of your rewrites is re-processed through httpd.conf, and again through any .htaccess files in the the newly-rewritten path; This must be done so that any further rewrites and/or access controls for the new path can be applied. Therefore, you must explicity prevent rewrite and redirection loops in .htaccess, usually using RewriteCond.


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*[^/])$ /$1/ [L]
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/user/index\.php$
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.+)$ /user/index.php?page=$1&args=%1 [L]

Jim

jahndi

7:52 am on Mar 8, 2005 (gmt 0)

10+ Year Member



Excellent! It's almost working as planned now. Thankyou so much.

Just one more thing, though. Can you tell me why this regular expression

url: [mysite.com...]

RewriteRule ^([a-zA-Z0-9_]+)\/?([a-zA-Z0-9_]+)\/?([a-zA-Z0-9_/]+)$ /admin_dir/admin_index.php?page=$2&args=$3

keeps giving me values of page=hom args=e?

I've tried all sorts of variations, but the only one that works is when I escape the foward slashes. Why is this?

Thank you so much in advance.

jdMorgan

3:56 pm on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know why you're having to escape the slashes, it is not required by the regular expressions parsing in mod_rewrite.

In this rule, you have three subpatterns:

RewriteRule ^([a-zA-Z0-9_]+)\/?([a-zA-Z0-9_]+)\/?([a-zA-Z0-9_/]+)$ /admin_dir/admin_index.php?page=$2&args=$3

While in this URL, you have only two substrings (bolded) that can be matched into those sub-patterns:

url: http://www.mysite.com/admin/home

However, you have made the forward slashes optional by following them with "?". Therefore, the substrings will be assigned to the subpatterns as:
$1 admin (not used)
$2 hom
$3 e


If you want a 'strict' rule that accepts only requests with three substrings, then use something like this:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /admin_dir/admin_index.php?page=$2&args=$3

The [^/]+ subpatterns mean, "match one or more characters not equal to a slash."

I note also that you have discarded the %1 variable, which contained your original query string in the code I posted.

Jim

jahndi

6:24 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



I discarded it because I wasn't sure what it's purpose was. On top of that, the variables it was handing me when not in /admin/ were just right and it worked flawlessly.

in www.mysite.com/admin/page/list/of/vars
I can't really set it up so strictly because of the way I'm handling the variables. /admin/ tells it of course that we're in the admin section, page is pulled as one GET variable that tells the template engine which page to pull, and list/of/vars is another GET variable, which is split by "/" and used as optional variables for the page. So by that, there aren't any required variables. If the directory is simply /admin/, then the script defaults to the index page for that. If it's /admin/page/, then that page is loaded without any variables. Can you explain to me where the %1 was being used? Thankyou again so much for your help.

jdMorgan

8:37 pm on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On the query string stuff in variable %1, that may have been a red herring... It's sometimes hard to discern intent in the posts here.

jahndi

2:25 am on Mar 22, 2005 (gmt 0)

10+ Year Member



Alright! So everything is working ALMOST as planned. When using URL rewriting, do you typically have to use absolute paths for images and styles? Relative ones seem to break down for me. Thanks again, you've been a great help.

jdMorgan

2:35 am on Mar 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, because the browser resolves relative paths relative to where *it* thinks the directory is, and that may be different if you are rewriting.

However, you can use paths relative to your document_root instead of relative to the page. That is, use
<img src="/page_dir/image.gif"> instead of <img src="image.gif"> or <img src="../image.gif">.

Or, you can rewrite the image URLs, if that's more convenient for your site design.

Jim

jahndi

4:59 am on Mar 24, 2005 (gmt 0)

10+ Year Member



You've been such an amazing help so far I thought I would ask you one more thing.

So, my local file/web server's base directory (as set in httpd.conf) is

/home/newgonzo/windows/dev

and within the dev directory I have different folders with different client sites in development. In each of those site folders, the .htaccess you've been helping me with resides. It works great when it's posted to the client's webserver (and the site's base directory is set right) but I can't get it to work on mine. It keeps going back to /dev as the base directory instead of /dev/client_folder. Obviously, this causes problems. I've tried several things with a RewriteBase in the .htaccess in the client's folder. Do you have any suggestions?

Thanks again, you're a great help.

John