Forum Moderators: phranque
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
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]
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.
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
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.
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
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