3

I would like to add a bit of .htaccess wizardry to an existing .htaccess file (see https://github.com/silverstripe/silverstripe-installer/blob/master/.htaccess):

<IfModule mod_rewrite.c>
    SetEnv HTTP_MOD_REWRITE On
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* framework/main.php?url=%1 [QSA]
</IfModule>

so that if the URL starts with nz / au / us then this first segment is removed from the "url-get variable" created and added as its own get variable.

e.g.


[domain name goes here]/nz/my/page/is/here/

is redirected to:

main.php?url=/my/page/is/here/&country=nz


[domain name goes here]/au/my/other-page/is/here/

is redirected to:

main.php?url=/my/other-page/is/here/&country=au


[domain name goes here]/my/third-page/is/here/

is redirected to:

main.php?url=/my/third-page/is/here/

1 Answers1

3

You can do something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(nz|au|us)/?(.*)$ framework/main.php?url=$2&country=$1 [NC,L]

Keep in mind, that this will make the country-code a mandatory part of the URL and you might have to prefix all links you generate. Of course you could always add your original rewrite rule below as a fallback. So your complete set of rules will look like this:

# Match URL with country code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(nz|au|us)/?(.*)$ framework/main.php?url=$2&country=$1 [NC,L]

# Fallback to original rewrite-rule
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1 [QSA]
bummzack
  • 5,805
  • 1
  • 26
  • 45