RewriteRule for htaccess to hide default language

2019-06-11 12:47发布

问题:

In Zend configuration defaults I have these rules:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

When i try to add fallowing line, notfing happens:

RewriteRule ^en/(.*) $1 [L]

On result I expact to rewrite permomently http://example.com/en/something to http://example.com/something At now I have both links worked separatly.

Edited:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

RewriteRule ^en/(.*) /$1 [L,R]

This will redirects default language urls directly to site root! Many thanks.

There is for LigHTTPD:

$HTTP["url"] != "^/en/" {
    url.rewrite-once = (
        "^/.*" => "index.php?/$1",
    )
}
url.redirect = (
    "^/en/.*" => "/$1",
)

回答1:

It's bceause RewriteRule ^.*$ index.php [NC,L] is rewriting it and it never gets to the rule you added. You need to add a condition so that requests starting with /en/ don't get rewritten to index.php:

# you regular stuff
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Add this condition
RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

# Now you can rewrite /en
RewriteRule ^en/(.*) $1 [L,R]

Edited: example.com/en/something gets redirected to example.com/something, then rewritten to index.php.