Forum Moderators: phranque

Message Too Old, No Replies

extending a simple mod rewrite

mod_rewrite rules and conditions

         

radiofree

2:38 pm on Mar 25, 2008 (gmt 0)

10+ Year Member



I have a simple .htaccess mod_rewrite rules being applied, successfully, on a web site, such that:

www.site.com/en/products/

translates to:

www.site.com/index.php?lang=en&page=products

What I would like to do now, is add an additional rule (I guess) that would allow an additional, seeming sub-directory or sub-folder, e.g.:

www.site.com/en/products/
www.site.com/en/products/prices/
www.site.com/de/products/support/

translates to:

www.site.com/index.php?lang=en&page=products (same as before)
www.site.com/index.php?lang=en&page=products_prices
www.site.com/index.php?lang=de&page=products_support

In other words I'd like any forward slashes after the first one, to be converted to underscores and used as part of the page name... I am a mod_rewrite idiot though, so if someone can suggest how extending the following, I'd really appreciate it. Thanks!

------------
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_METHOD} ^trace$ [NC]
RewriteRule .* - [F,L]

# Remove .php if it's not a real file.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^((en¦de¦ru)/[a-z0-9-_]+)\.php$ /$1/ [NC,R=301,L]

# Force the trailing slash.
RewriteRule ^((en¦de¦ru)/[a-z0-9-_]+)$ /$1/ [NC,R=301,L]

RewriteRule ^(en¦de¦ru)/([a-z0-9-_]+)/$ /index.php?lang=$1&page=$2 [NC,QSA,L]

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

/// RF

jdMorgan

5:17 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then you'll need a more-specific rule in addition to your general rule:

RewriteRule ^(en¦de¦ru)/(prices¦support)/$ /index.php?lang=$1&page=product_$2 [NC,QSA,L]
RewriteRule ^(en¦de¦ru)/([a-z0-9-_]+)/$ /index.php?lang=$1&page=$2 [NC,QSA,L]

I'd look at this carefully if it's likely that you'll add yet more 'page categories' in the future. You might be better off modifying the script to accept "prices" and "support" directly, without the "products_" prefix in that case.

Change all broken pipe "¦" characters above to solid pipes before use; Posting on this forum modifies the pipe characters.

Completely flush your browser cache before testing any new code.

The [L] in [F,L] is redundant -- [F] alone is sufficient.

Jim

radiofree

5:42 pm on Mar 25, 2008 (gmt 0)

10+ Year Member



OK, that sort of makes sense. I suppose I should have added that I want the $page variable to potentially be any word with or without an underscore (a-z0-9-_). Pricing and support were just examples--the suffix in www.site.com/en/page_suffix/ could be anything. Is this still possible? Well, I'm sure it is, but I'm just wondering where the addition rule would go, at the end?

...

# Force the trailing slash.
RewriteRule ^((en¦de¦ru)/[a-z0-9-_]+)$ /$1/ [NC,R=301,L]

RewriteRule ^(en¦de¦ru)/(any suffix can go here)/$ /index.php?lang=$1&page=product_$2 [NC,QSA,L]
RewriteRule ^(en¦de¦ru)/([a-z0-9-_]+)/$ /index.php?lang=$1&page=$2 [NC,QSA,L]

jdMorgan

6:20 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure what your URL-space looks like, or what it will look like as you grow it. Maybe you're looking for something like this:

RewriteRule ^(en¦de¦ru)/([a-z0-9\-_]+)/([a-z0-9\-_]+)/$ /index.php?lang=$1&page=$2_$3 [NC,QSA,L]
RewriteRule ^(en¦de¦ru)/([a-z0-9\-_]+)/$ /index.php?lang=$1&page=$2 [NC,QSA,L]

The problem is not with coding the solution, it's with defining the problem precisely, so a solution can be coded.

As for the order of rules, generally you want external redirects first, in order from most-specific (or most-complex) pattern to least-specific pattern, followed by internal redirects -- again ordered from most-specific pattern to least-specific pattern.

In simple terms, redirects for single specific URLs go first, and "catch-all" redirects --such as all-page domain canonicalization-- go last. These are then followed by your internal rewrites.

Putting internal rewrites ahead of external redirects risks 'exposing' the internal filepaths to the client.

For more information on mod_rewrite and regular expressions, see the resources cited in our forum charter and the tutorials in our forum library (links at top of this page).

Jim