Page 1 of 1

Confused with htaccess RewriteEngine

Posted: Thu Apr 15, 2010 6:15 am
by Weiry
I have read up on using clean URL's through the use of .htaccess files.

Firstly http.conf:

Code: Select all

LoadModule rewrite_module modules/mod_rewrite.so
...
<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
</Directory>
Secondly my .htaccess file:

Code: Select all

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)/$ index.php?sort=$1&direction=$2 [NC,R,L]
RewriteRule ^([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)/$ ?sort=$1&direction=$2 [NC,R,L]
RewriteRule ^([a-zA-Z0-9]+)\/$1/$2 index.php?sort=$1&direction=$2 [NC,R,L]
RewriteRule ^([a-zA-Z0-9]+)\/$1/$2 ?sort=$1&direction=$2 [NC,R,L]
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?sort=$1 [NC,R,L]
RewriteRule ^([a-zA-Z0-9]+)/$ ?sort=$1 [NC,R,L]
I have tried so many different things, even just a direct copy/paste from different places using their exact variables etc and still the URL remains the same.
eg: http://localhost/index.php?sort=class&direction=asc
http://localhost/class/asc

Can anyone suggest why i just can't get my .htaccess file to work properly?

Re: Confused with htaccess RewriteEngine

Posted: Thu Apr 15, 2010 8:31 am
by solid
1. Since you are setting FollowSymLinks in your vhosts file, you don't need to set it again, so that line can be removed.
2. You don't need to escape the first forward-slash (note that you were trying to escape the first, but not the second)
3. You don't need [NC] as you are specifying A-Aa-a anyway
4. You don't need [R] as you do not appear to be using external redirection
5. Remember that if one [L] line matches, then that will be the address used, all further lines are ignored, so you cannot have duplicate patterns as you do, because the first pattern match is the only one that will ever be hit.
6. Note that you aren't matching any dashes (-)or underscores (_), so any addresses which include them will not be redirected.

Try this:

Code: Select all

RewriteEngine On
RewriteRule ^([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)/$ index.php?sort=$1&direction=$2 [L]
RewriteRule ^([a-zA-Z0-9\-_]+)/$ index.php?sort=$1 [L]

Re: Confused with htaccess RewriteEngine

Posted: Thu Apr 15, 2010 9:31 am
by Weiry
The URLs im using don't have underscores or dashes, which is why i wasn't searching for them.
solid wrote:Try this:
Ok i replaced my .htaccess file with what you included, and still not much luck. I still receive the original URL.
(Note: i modified the GET direction to 'dir' and made appropriate changes to the code)
Whenever i load the url: http://localhost/?sort=class&dir=desc it remains the same.

Code: Select all

RewriteEngine On
RewriteRule ^([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)/$ index.php?sort=$1&dir=$2 [L]
RewriteRule ^([a-zA-Z0-9\-_]+)/$ index.php?sort=$1 [L]

Re: Confused with htaccess RewriteEngine

Posted: Thu Apr 15, 2010 10:58 am
by AbraCadaver
I may be confused, but it seems you are thinking about this backwards.

In your example, this is what you put in the browser location bar: http://localhost/class/desc/

And the web server will rewrite it and request: http://localhost/?sort=class&dir=desc

Re: Confused with htaccess RewriteEngine

Posted: Thu Apr 15, 2010 11:34 am
by Weiry
Ive just been going off examples and they all seem to put them like that :?
But your right,
i want: ?sort=blah&dir=asc
into: /blah/asc

maybe?

Code: Select all

RewriteRule ^index\.php\?sort=([a-zA-Z0-9\-_]+)\&dir\=([a-zA-Z0-9\-_]+)/$ $1/$2

i dont think i like .htaccess stuff anymore

Re: Confused with htaccess RewriteEngine

Posted: Thu Apr 15, 2010 12:06 pm
by AbraCadaver
No, I didn't mean that the rewrite rules were backwards but maybe how you think they work. Here is an example:

A link on your web page:
[text]<a href="search.html">Search Now</a>[/text]
.htaccess
[text]RewriteRule ^search.html index.php?action=search[/text]
When you click the link and the web server gets the request for http://localhost/search.html it will internally (and hidden from you) rewrite it to http://localhost/index.php?action=search and request that, but you will still see http://localhost/search.html in the browser address bar.

It doesn't create "clean" URLs for you. You supply the "clean" URLs and it rewrites them to the proper one.