Forum Moderators: phranque
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.
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]
Ref: Introduction to mod_rewrite [webmasterworld.com]
Jim
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?
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]
Jim