Confused with htaccess RewriteEngine

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Confused with htaccess RewriteEngine

Post 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?
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

Re: Confused with htaccess RewriteEngine

Post 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]
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Confused with htaccess RewriteEngine

Post 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]
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Confused with htaccess RewriteEngine

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Confused with htaccess RewriteEngine

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Confused with htaccess RewriteEngine

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply