Mr_Invisible
asked on
Apache rewrite rules and various sub-directories
I have a quick question about rewrite rules that I havn't been able to crack all day. The problem is thus:
I have a members directory located under / (ie. http://xxx.com/Members/) and I've allowed the members access simply by xxx.com/mymembername/ (redirecting all requests to /Members - no probs), but the thing is ALL requests are processed this way (including requests to /Images, or if they enter /Members/xxx anyway etc). How can I stop this from happening? My rewrite rules thus far:
# don't re-direct /Member/ or /Images/
RewriteCond ^\/([^\/]+) ^\/Members.* [OR]
RewriteCond ^\/([^\/]+) ^\/Images.*
RewriteRule ^\/([^\/]+) - [L]
# redirect everything else
RewriteRule ^\/([^\/]+)(\/.*) /var/www/xxx.com/Members/$ 1$2 [L]
What am I missing? Thanks in advance.
/me
I have a members directory located under / (ie. http://xxx.com/Members/) and I've allowed the members access simply by xxx.com/mymembername/ (redirecting all requests to /Members - no probs), but the thing is ALL requests are processed this way (including requests to /Images, or if they enter /Members/xxx anyway etc). How can I stop this from happening? My rewrite rules thus far:
# don't re-direct /Member/ or /Images/
RewriteCond ^\/([^\/]+) ^\/Members.* [OR]
RewriteCond ^\/([^\/]+) ^\/Images.*
RewriteRule ^\/([^\/]+) - [L]
# redirect everything else
RewriteRule ^\/([^\/]+)(\/.*) /var/www/xxx.com/Members/$
What am I missing? Thanks in advance.
/me
ASKER
My Apologies, I forgot I had started this thread. I found an answer, and in the event anyone else has the problem my solution is below. BTW I tried negating it, but by the looks of it I was on the wrong track anyway.
<IfModule mod_rewrite.c>
# we want to be able to have the member name as a root folder, but this is messy if we create them in the root folder.So we use mod_rewrite to check if the REQUEST_FILENAME is a directory or file under the /Members/ folder - if it is, then redirect, otherwise check for other patterns.
# Re-Write trickery
RewriteEngine on
RewriteLog "/var/log/apache/xxx.com/r ewrite.log "
RewriteLogLevel 1
# redirect these if they exist
RewriteCond /var/www/xxx.com/Members%{ REQUEST_FI LENAME} -d [OR]
RewriteCond /var/www/xxx.com/Members%{ REQUEST_FI LENAME} -f
RewriteRule ^/(.*) http://xxx.com/Members/$1 [L]
# Don't redirect these
RewriteRule ^/Images/ - [L]
RewriteRule ^/Members/ - [L]
RewriteRule ^/inc/ - [L]
RewriteRule ^/MyCMS/ - [L]
RewriteRule ^/Admin/ - [L]
RewriteRule ^/test/ - [L]
# redirect '/xxx/' (assuming xxx is a member) - last chance to redirect something
RewriteRule ^\/([^\/]+)(\/.*) http://xxx.com/Members/$1$2 [L]
</IfModule>
This is what worked for me, the only downside was if the member had an index file the same name as a root file, it would get that instead.
Now, is there anyway to close this thread, or does someone just kill it?
/me
<IfModule mod_rewrite.c>
# we want to be able to have the member name as a root folder, but this is messy if we create them in the root folder.So we use mod_rewrite to check if the REQUEST_FILENAME is a directory or file under the /Members/ folder - if it is, then redirect, otherwise check for other patterns.
# Re-Write trickery
RewriteEngine on
RewriteLog "/var/log/apache/xxx.com/r
RewriteLogLevel 1
# redirect these if they exist
RewriteCond /var/www/xxx.com/Members%{
RewriteCond /var/www/xxx.com/Members%{
RewriteRule ^/(.*) http://xxx.com/Members/$1 [L]
# Don't redirect these
RewriteRule ^/Images/ - [L]
RewriteRule ^/Members/ - [L]
RewriteRule ^/inc/ - [L]
RewriteRule ^/MyCMS/ - [L]
RewriteRule ^/Admin/ - [L]
RewriteRule ^/test/ - [L]
# redirect '/xxx/' (assuming xxx is a member) - last chance to redirect something
RewriteRule ^\/([^\/]+)(\/.*) http://xxx.com/Members/$1$2 [L]
</IfModule>
This is what worked for me, the only downside was if the member had an index file the same name as a root file, it would get that instead.
Now, is there anyway to close this thread, or does someone just kill it?
/me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just to clean things up a bit, and to make things easier for you, why aren't you just writing your rewrite conditions in the negative (with the ! ), like this:
RewriteCond %{REQUEST_URI} !^/Members/
RewriteCond %{REQUEST_URI} !^/Images/
RewriteRule ^\/([^\/]+)(\/.*) /var/www/xxx.com/Members/$
Does this help?
Some places to reference:
http://httpd.apache.org/docs/mod/mod_rewrite.html
http://httpd.apache.org/docs/misc/rewriteguide.html