homepage Welcome to WebmasterWorld Guest from 23.20.63.27
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
RewriteRule in .htaccess file
To make url becomes shorter
kurma




msg:1511946
 9:33 am on Jan 13, 2004 (gmt 0)

Hi.. I'm glad to find this forum, and the topic Introduction to mod rewrite really helps me.

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.

 

jdMorgan




msg:1511947
 10:21 am on Jan 13, 2004 (gmt 0)

kurma,

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
the result would be /ab/a/b

I hope that's clear - it's a little tricky.

Jim

kurma




msg:1511948
 6:47 am on Jan 14, 2004 (gmt 0)

Thanks jdMorgan. It works fine and doesn't loop anymore. Could you please elaborate what does this phrase mean?
######################################
RewriteCond %{REQUEST_URI}!^/members
######################################

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?

jdMorgan




msg:1511949
 7:43 am on Jan 14, 2004 (gmt 0)

kurma,

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

kurma




msg:1511950
 5:01 am on Jan 16, 2004 (gmt 0)

Just to share something here..

#######################################
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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved