我需要一个域来管理语言(en / it / pt / es)以及删除www。并强制https
RewriteEngine On # -> www.example.com to example.com RewriteCond %{HTTP_HOST} www.example.com RewriteRule (.*) http://example.com/$1 [L,R=301] # -> http -> https RewriteCond %{SERVER_PORT} 80 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
我不知道如何制作此重写规则: index.php?lg = es - > / es /而不是/ es或/es.php,甚至拒绝index.php?lg = es
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?lg=$1
...
搜索引擎优化对我的网站不利,只有英语被引擎所知,其他语言都在第4页的结果中。
之所以这是因为我有两种显示网址的方法吗?
example.com/it/和example.com/it不应该返回相同的页面
感谢您的帮助。
编辑(我认为一切正确吗?):10月7日感谢所有答案:RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com$ [OR] RewriteCond %{SERVER_PORT} 80 [OR] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L] RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{REQUEST_URI} ^/?index.php$ RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$ RewriteRule ^/?index.php$ - [F] ErrorDocument 404 /404.php
只是一个问题:测试https关闭是否真的有用(连接速度慢?)?感谢
2018年2月8日编辑
使用数月后会出现很多404错误。 Htaccess需要更简化。
我只需要:
1) www -> non-www
2) http -> https
3) index.php -> example.com
4) redirect index.php?lg=it and index.php?lg=es and index.php?lg=pt
to example.com/it or to example.com/es or to example.com/pt
如果它不是这种语言之一 - >去主页example.com或404 ??但谷歌网站管理员将增加404错误...
我需要一个非常简化的htaccess版本,其中有3种语言,其中有Rewrite Base吗?或不 ?
这是我的htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L]
RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?index.php$
RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$
RewriteRule ^/?index.php$ - [F]
ErrorDocument 404 /404.php
一切都在index.php中管理,创建3个页面更好吗?
index.php - >重定向到example.com
es.php - >重定向到example.com/es
it.php - >重定向到example.com/it删除.php
有人能成功吗?感谢
答案 0 :(得分:1)
此解决方案很好,因为可以应用于任何域(没有硬代码名称)。
一些注意事项:
请注意,4和5在public / folder中有index.php文件...如果需要,可以更改或删除它。
# ###############################################################################
# | Rewrite engine (SEO URLs friendly) |
# ###############################################################################
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
## Process pages requests
# 1. To redirect from www to non www (Rewrite www.example.com → example.com)
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
# 2. Redirect HTTP to HTTPS automatically (only if not in localhost)
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 3. (Add a slash) aa → aa/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)$ $1/
# 4. (Default homepage) aa/ → ?lang=aa&page=home
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/$ public/index.php?lang=$1&page=home
# 5. (Language and page) aa/bb → ?lang=aa&page=bb
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/([^./]+)$ public/index.php?lang=$1&page=$2
</IfModule>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
答案 1 :(得分:0)
这应该对你有用:
RewriteEngine On
# redirect to none-www and https
# if host do not starts with www.
RewriteCond %{HTTP_HOST} ^www\.(([A-Za-z0-9][A-Za-z0-9-]+\.)+[A-Za-z]{2,})$ [OR]
# or if Port is 80
RewriteCond %{SERVER_PORT} 80 [OR]
# or if https is off
RewriteCond %{HTTPS} off
# redirect to example.com with https
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Redirect Language Strings without Trailing Slashes
RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L]
# Rewrite Language string
RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC]
# Prevent direct access to /index.php?lg=$1 [QSA,NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?index.php$
RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$
RewriteRule ^/?index.php$ - [F]
我用Apache 2.4.27测试了这个Ubuntu 16.04.03 LTS的正面