Forum Moderators: phranque

Message Too Old, No Replies

Simple .htaccess mod rewrite question

Simple .htaccess mod rewrite question

         

PumpkinHead

1:26 pm on Oct 3, 2005 (gmt 0)

10+ Year Member



I have a kind of directory on my site that contains 3 different levels:

a) Welcome Page (Displays a A-Z list of website prefixes)
b) Displays listing of websites for the selected prefix (E.g "S")
c) Displays details of selected website (E.g "Simon's website")

So...

index.php ---> /
index.php?list=s ---> /s/
index.php?list=s&site=simon---> /s/simonswebsite/

Can somone please advise how I can do this, the problem is that the site name could also be the same as the prefix (E.g index.php?list=s&site=s).

My current code...

----------------------
RewriteEngine on
RewriteRule ^index\.html$ /index.php [L]
RewriteRule ^([a-z]{1})/?$ /index.php?list=$1 [L]
----------------------

Thanks all, hope someone can help.

jdMorgan

4:06 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need some way to uniquely discriminate the URLs that need to be rewritten from those that should not be rewritten.

The least painful solution is to disallow the use of single-character names for anything except your 'prefix directory section' URLs. Or you could tag the prefix directory section URLs with a unique URL-path prefix or extension.

mod_rewrite isn't magic; It depends on matching information in the HTTP request from the client (e.g. browser) to decide whether and what action to take. So you have to provide sufficient information in the URL or query string to make this decision.

Jim

PumpkinHead

12:00 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



I think I'm getting the hand of this, but I'm still having problems re-writing the following url:

www.mydomain.com/list_s/site_example/

which I need to point to:

index.php?list=s&site=example

Do I have a seperate .htaccess line for re-writting 'list_s' and another line for re-writting 'site_example' or do I do this on one line?

jdMorgan

3:33 pm on Oct 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What trouble are you having?

Static case:


RewriteRule ^list_s/site_example/ /index.php?list=s&site=example [L]

Fully-Dynamic case:

RewriteRule ^([^_]+)_([a-z])/([^_]+)_([^/]+)/ /index.php?$1=$2&$3=$4 [L]

The construct ([^_]+) means "require at least one character not equal to an underscore and create a back-reference to this value." A similar technique is used for the slash at the end. In this way, the incoming request URI can be parsed in one pass from left to right, rather than recursively, as would be required with a (.*) pattern.

Jim

PumpkinHead

11:14 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Hi,

I'm not sure what I'm doing wrong but it's probably something stupid.

Here is my .htaccess in full:

------------------------------------------------------

RewriteEngine on
## "index.html" requests "index.php"
RewriteRule ^index\.html$ /index.php [L]

## "/list_other/" requests "/index.php?list=other"
RewriteRule ^([^_]+)_([^_]+)/ /index.php?$1=$2 [L]

## "/list_other/site_example/" requests "/index.php?list=other&site=example"
RewriteRule ^([^_]+)_([^_]+)/([^_]+)_([^_]+)/ /index.php?$1=$2&$3=$4 [L]

------------------------------------------------------

/ works fine (directs to /index.php)

/list_other/ works fine (directs to /?list=other)

/list_other/site_example/ doesn't work, my code still can't find the value of $site

wizzud

12:14 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



I suspect that's because your third example (the one that doesn't work) is matching against the second rule, because the second rule doesn't say that the incoming path ends with that pattern, just that it needs to begin with it. Hence both /list_s/ and /list_s/anything_else/ will match the second rule.