Forum Moderators: phranque
I was assigned to do some research on how to make a url shorter. This applies to user directory in a website builder. I think the best example is from this URL (http://httpd.apache.org/docs/misc/rewriteguide.html), in the subtopic Structured Homedirs.
What I want to do is to make a url from:
[mydomain.com...] to
[mydomain.com...]
This is my RewriteRule where it works fine only for index file:
#################################################
RewriteEngine on
RewriteRule ^(([a-z])[a-z0-9]+)/([a-z0-9_]+)$ /members/$2/$1/$3/index.html
#RewriteRule ^(([a-z])[a-z0-9]+)(.*) /members/$2/$1$3 ##################################################
The 1st one just a try. It works ok.. but the effect is, I can't go to non-user directory/file, that is directory outside of members/.
The 2nd one doesn't work. The browser keep searching the page, no replies and I have to wait for a long time but nothing happen.
I would really appreciate for any help. Thank you.
Welcome to WebmasterWorld [webmasterworld.com]!
The usual way to stop a server loop like you describe is to precede the RewriteRule with an exclusionary RewriteCond. In your case, this would be:
RewriteCond %{REQUEST_URI} !^/members I also want to warn you that with your parentheses nested the way you have them, $1 includes both $2 and $3 -- backreferences are assigned in the order that left parentheses "(" appear in the pattern.
Thus, if you have a rule like
RewriteRule ^((a)(b))/(c)$ /$1/$2/$3 I hope that's clear - it's a little tricky.
Jim
I'm still searching the topic discussion on RewriteCond with a clear example, but I don't find it yet. What does character '!' mean?
If I can go to [mydomain.com...] , with the current RewriteRule, I'm not able to go to [mydomain.com...]
To make things clear, the structure is like this:
/members/j/john/index.html
/members/k/kate/test.php
/my_cat/index.html
With current RewriteRule, I will get message "The page cannot be found" from the web browser if I go to my_cat/index.html. I hope our friends here will give guidance on how to make exception for non-members directory like "my_cat/". I guess, I have to write more RewriteCond, haven't I?
Start here with DaveAtIFG's tutorial, Introduction to mod_rewrite [webmasterworld.com].
Follow the links to the Apache documentation (which will tell you that "!" means NOT), and to the regular-expressions tutorial.
"RewriteCond %{REQUEST_URI} !^/members" means, "If the requested URI (URL) does not start with "/members", then apply the following RewriteRule."
You are correct. You can use multiple RewriteCond directives to redirect based on subdirectory. Alternately, you can use multiple RewriteRules - it all depends on which is easier for you and which is most efficient.
Jim
#######################################
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/members
RewriteCond %{REQUEST_URI}!^/non_members_dir_1
RewriteCond %{REQUEST_URI}!^/non_members_dir_2
RewriteCond %{REQUEST_URI}!^/non_members_dir_3
RewriteRule ^(([a-z])[a-z0-9]+)(.*) /members/$2/$1$3
#######################################
This will redirect
[mydomain.com...] TO
[mydomain.com...]
Err.. unfortunately, I can't go to [domain.com....] Maybe I should put
##################################
RewriteCond %{REQUEST_URI}!^/
##################################
but it didn't work. If I know the solution, I'll post it here :)
By the way, I leave that issue, because another assignment is to make
[subdomain.domain.com...] TO
[john.subdomain.domain.com...] (OR [john.domain.com...]
From (http://httpd.apache.org/docs/misc/rewriteguide.html), I tried to make the virtual user host, but didn't work.
###########################################
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[a-z0-9]+\.subdomain\.domain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(([a-z])[a-z0-9]+)\.subdomain\.domain\.com(.*) /members/$2/$1$3
############################################
Do I need to edit my httpd.conf at <VirtualHost> or is it just simply edit .htaccess file?
Thanks.