Hi I have found many resources on mod-rewrite and Apache but I haven't been successful of making a working example.
The example below works for
✓ WORKS example.com/en/page1.php ☞ example.com/page1.php?ln=en (internal redirect)
✓ WORKS example.com/page1.php ☞ example.com/en/page1.php (redirect)
✓ WORKS example.com/en/ ☞ example.com/index.php?ln=en (internal redirect)
✓ WORKS example.com/ ☞ example.com/en/ (redirect)
✓ WORKS example.com/blahblah ☞ example.com/en/error.php (redirect)
✓ WORKS example.com/en/blahblah ☞ example.com/en/error.php (redirect)
✖ FAILS example.com/de ☞ example.com/en/error.php (redirect - SHOULD BE example.com/de/)
✖ FAILS example.com/de/blahblah ☞ example.com/en/error.php (redirect - SHOULD BE example.com/de/error.php)
<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash On
# Force hostname without www REDIRECT
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://%1/$1 [R=301]
# /xx/somepage.php -> /somepage.php?ln=xx INTERNAL REWRITE
RewriteCond %{REQUEST_URI} ^/../.+ [NC]
RewriteRule ^(en|de)/(.*) $2?ln=$1&%{QUERY_STRING} [L]
# /en -> /en/ REDIRECT
RewriteCond %{REQUEST_URI} ^/..$ [NC]
RewriteRule ^(.*)$ $1/ [R=302,L]
# /somepage.php -> /en/somepage.php REDIRECT
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteRule ^(.*)$ /en%{REQUEST_URI} [R=302,L]
# /en/ -> /en/index.php INTERNAL REWRITE
RewriteCond %{REQUEST_URI} !^/../.+ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteRule ^(.*)$ /$1index.php [L]
</IfModule>
★ NOTE 1: R=302
is for debugging. On server it's actually R=301
.
★ NOTE 2: It's on Apache 2.2 so the %{ENV:REDIRECT_STATUS} !=200
bits are trying to emulate the [END]
flag of the later Apache 3.2.9.
Thanks!
★ EDIT: Here is more of the .conf file
<Directory /var/www/vhosts/example.com/httpdocs>
Options +FollowSymLinks -Indexes
AllowOverride All
AddDefaultCharset utf-8
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 402 /error.php
...
ErrorDocument 500 /error.php
[above code here]
</Directory>