Forum Moderators: phranque
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
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]
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
...
# 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]
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]
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