Forum Moderators: phranque

Message Too Old, No Replies

simple rewrite help

         

bwlange3

3:41 pm on Oct 28, 2006 (gmt 0)

10+ Year Member



I have been trying for hours now to get what should be a simple rewrite to work. I am new to this so please be patient with me.

I am trying to rewrite a url like this "www.example.com/sectionname/categoryname/pageidname" to "www.example.com/index.php?section=sectionname&category=categoryname&pageid=pageidname"

this should be fairly simple from what I have read, but I cannot get it to work. I found this:

"RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?product=$1&color=$2&size=$3&texture=$4&maker=$5 [L]"

but when I reformat it to work for me it doesn't work and I get a 404 error.

My .htaccess file:

"RewriteEngine on
RewriteBase /directory/
RewriteCond %{REQUEST_URI}!/index\.php

RewriteCond %{REQUEST_URI}!\.jpg$
RewriteCond %{REQUEST_URI}!\.png$
RewriteCond %{REQUEST_URI}!\.gif$
RewriteCond %{REQUEST_URI}!\.css$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /direcotry/index.php?section=$1&category=$2&pageid=$3 [L]"

if I write it like this it works:

"...
RewriteRule ^([^/]+)/?$ /directory/index.php?section=$1 [L]"

but i cannot get it to work with more than one variable. Please help. Thanks very much in advance.

jdMorgan

4:38 pm on Oct 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to study up on regular expressions to understand why your code won't work. As written, it requires five "attribute" URL-path-parts, and your specified test URL contains only three. Each of those "([^/]+)/" patterns must be matched by a "piece" of your request URL; Otherwise, the rule will not be invoked. Each of those patterns will pass the matched "piece" of the URL in a numbered "$" variable, so that that "piece" can be re-used in the new URL. The term for this is "back-reference."

For url /sectionname/categoryname/pageidname, and all 'shorter' URL versions, I'd suggest:


RewriteCond %{REQUEST_URI} !(\.(jpg¦png¦gif¦css)¦/index\.php)$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /directory/index.php?section=$1&category=$2&pageid=$3 [L]
#
RewriteCond %{REQUEST_URI} !(\.(jpg¦png¦gif¦css)¦/index\.php)$
RewriteRule ^([^/]+)/([^/]+)/?$ /directory/index.php?section=$1&category=$2 [L]
#
RewriteCond %{REQUEST_URI} !(\.(jpg¦png¦gif¦css)¦/index\.php)$
RewriteRule ^([^/]+)/?$ /directory/index.php?section=$1 [L]

Or alternatively, if there are many more URL-length variations and the above approach gets unwieldy:

RewriteRule index.php$¦\.(jpg¦png¦gif¦css)$ - [S=3]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /directory/index.php?section=$1&category=$2&pageid=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /directory/index.php?section=$1&category=$2 [L]
RewriteRule ^([^/]+)/?$ /directory/index.php?section=$1 [L]

Since you've used the RewriteBase directive, I assume that you've done so in an informed manner, and that requests for "/" in this domain actually resolve to <document_root>/directory/ on your server. If not, then your rules won't work as expected: Refer to the RewriteBase documentation.

Replace all broken pipe "¦" characters in the code above with solid pipe characters (Shift-\ on most U.S. keyboards) before use; Posting on this forum modifies the pipe characters.

If you get 404 errors (or any others), examine your server error log -- The information it contains will often tell you exactly what is wrong.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim