Old 08-02-2008, 04:33   #1 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,931
.htaccess navigation

I am trying to set up navigation by directory address similar to Wordpress.

So

mydomain.com/hello

goes to

mydomain.com/index.php?var=hello

I have got this far but not quite right, any clues? Thanks.

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/ index.php?var=$1

__________________
  Reply With Quote
Old 08-02-2008, 05:18   #2 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Code:
Options +FollowSymlinks RewriteEngine on RewriteRule ^hello/?$ index.php?var=hello

If you've a range of sections/pages, you can match is against a pattern.

e.g.
Code:
RewriteRule ^([page1|page2|page3|page4])/?$ index.php?var=$1

…or…

Code:
RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?var=$1
This last one works with any 'directory' which consists of those characters listed between the square brackets: 0-9, a-z upper and lowercase, hyphen and underscore.
  Reply With Quote
Old 08-02-2008, 05:47   #3 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,931
Thanks Bill, unfortunately it won't work.

The first one index.php is not reacting to the variable and the second crashes the server for all files.

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
__________________
  Reply With Quote
Old 08-02-2008, 05:53   #4 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
The second one contains an error. (Oops!)
The hyphen near the end should be the first character…

Code:
RewriteRule ^([-a-zA-Z0-9_]+)/?$ index.php?var=$1

The use of the var name var may also be causing issues, as it may be a protected keyword. It's worth using another name for the parameter, just to be safe.


On the index.php, you'd access the parameters using $_GET['paramName'] (paramName being whatever name you use for the parameter.
  Reply With Quote
Old 08-02-2008, 06:07   #5 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,931
Oh yes it works now, and you were right I was not using $GET.

Creaming my pants.

FWIW Cannot make the first one work though.
__________________
  Reply With Quote
Old 08-02-2008, 11:01   #6 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Quote:
Originally Posted by datahound
FWIW Cannot make the first one work though.

This one…?
Code:
RewriteRule ^hello/?$ index.php?var=hello

…or this one…?
Code:
RewriteRule ^([page1|page2|page3|page4])/?$ index.php?var=$1

In both cases, the rule will only work for exact matches.
The hello one will only forward a request from yourdomain.com/hello or yourdomain.com/hello/ to yourdomain.com/index.php?var=hello

The second one will only forward the following url requests
Code:
yourdomain.com/page1 yourdomain.com/page1/ yourdomain.com/page2 yourdomain.com/page2/ yourdomain.com/page3 yourdomain.com/page3/ yourdomain.com/page4 yourdomain.com/page4/
.
  Reply With Quote
Old 08-02-2008, 11:09   #7 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,358
I like the way Drupal handles things.

Code:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It does this nice thing with creating a single query string for every request:

Code:
somewhere.com/index.php?q=module/function/something
Becomes:

Code:
somewhere.com/module/function/something
Similar to the way some MVC frameworks seem to work (but then I'm not too familiar with them either as I only really dabble in server-side stuff).
  Reply With Quote
Old 08-02-2008, 11:41   #8 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 4,931
It was this one Bill

RewriteRule ^([page1|page2|page3|page4])/?$ index.php?var=$1

When I change page1 for hello it should function but does not.

But no never mind I am entirely content with the other.

Thanks.
__________________
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8