RewriteRule for htaccess to hide default language

旧城冷巷雨未停 提交于 2019-12-08 09:54: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.



来源:https://stackoverflow.com/questions/8133094/rewriterule-for-htaccess-to-hide-default-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!