homepage Welcome to WebmasterWorld Guest from 54.161.202.234
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
rewrite rule
modifying urls
nippi




msg:1516238
 1:33 am on Oct 29, 2003 (gmt 0)

I am new to regex and all that, and find it a bit heavy...
I am trying to write a rule that will change a url like:
mydomain.com/page_name/item_name/item_id/page_number/

and convert it to mydomain.com/page_name.php?id=item_id&page=page_number

I want this to happen only when the requested url is a directory (does not have an extension, so a url like mydomain.com/page1.php will still work normaly)

any genius out there who can help me?

Thanks heaps.

 

BlueSky




msg:1516239
 3:00 am on Oct 29, 2003 (gmt 0)

-- nm --

[edited by: BlueSky at 3:42 am (utc) on Oct. 29, 2003]

jdMorgan




msg:1516240
 3:29 am on Oct 29, 2003 (gmt 0)

nippi,

As stated in our charter [webmasterworld.com], we prefer not to do "write my code for me"-type threads here, but rather to help you learn how to do it. We'd prefer that you post your "best attempt" and ask specific questions here to help get it working. In this way, the thread is useful to you *and* to others who have the same or similar problems.

However, the cat's out of the bag now, so I'll attempt to use this thread to provide some generally-useful information.

Your case is interesting in that you say you want to bypass the rule if the incoming URL contains a file-type. So here's one way to do that, using RewriteCond:

# if local path does not contain a dot (implies no file extension)
RewriteCond %{REQUEST_URI} !\.
# redirect to appropriate script, remove other variables from URL to query string
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$3&page=$4 [L]

The rule above discards item_name, as did your description. Each parenthesized group 'captures' a part of the local path bounded by slashes and copies it into a local variable designated '$1' up through '$9'. Given the URL you describe, $1 corresponds to page_name, $2=item_name, $3=item_id, and $4=page number. The values of these local variables are then used to populate the substitution in their designated spots. The trailing slash is made optional by the '?' in the pattern. The '[^/]+' construct is used to prevent a "greediness" problem, where '.*' can break a rule because it will match as many characters as possible. The [L] flag stops the rewriting engine if the pattern is matched and the redirect is performed, saving wasted CPU time.

Ref: Introduction to mod_rewrite [webmasterworld.com]

Jim

nippi




msg:1516241
 4:27 am on Oct 29, 2003 (gmt 0)

first of all: thanks. I was too embarrassed to post my attempts, and seeing how concise your lines are I know that i was completely on the wrong track.

The first suggestion from BlueSky works. Except that I had to change the variables numbers (I can't figure out why):
RewriteEngine on
RewriteRule (.*)/(.*)/(.*)/(.*) $1.php?id=$2&page=$3 [L].

jdMorgan: your sample seems thorough, but my page doesn't read the id and page_number parameters when I use it. I get the correct file, but doesn't seem to have the query string.

Is it unsafe to use BlueSky version?

jdMorgan




msg:1516242
 4:42 am on Oct 29, 2003 (gmt 0)

nippi,

The version of the code I posted will require the "item name" field to be present, as you stated in your examle.
mydomain.com/page_name/item_name/item_id/page_number/ -> mydomain.com/page_name.php?id=item_id&page=page_number

I suspect you don't actually have item_name in your URL, so therefore, you had to renumber the back-reference variables in BlueSky's code.

You will need to add the initial RewriteCond to BlueSky's code if you wish to prevent redirection when the URL contains a filetype, or perhaps try this version that does not require the item_name in the URL:

RewriteCond %{REQUEST_URI} !\.
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&page=$3 [L]

If you need to cover the situation where there *is* an optional item_name in the URL, then that will require a few more changes.

Jim

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