具有多个RewriteRules的复杂htaccess

时间:2014-01-24 17:43:26

标签: .htaccess mod-rewrite url-rewriting

在为我的网站配置.htaccess时遇到问题。我会尽力解释一下:

  • 假设我的网站是:example.com
  • 我的网站提供英语或西班牙语内容
  • 两种语言的页面相同。只有语言字符串不同。

我尝试的第一件事是强制使用www和https:

 - if user types http://example.com
 - if user types https://example.com
 - if user types http://www.example.com
he is always redirected to https://www.example.com

现在我尝试“美化”我的网站。我正在尝试为英语访问者提供example.com,为西班牙语访问者提供example.com/es。语言由查询字符串“language = english”或“language = spanish”应用。我想制作内部重定向。

我正在尝试在访问不同页面时应用这些规则。我将尝试公开所有可能的重定向:

  • example.com,example.com / example.com/index.php - >的index.php?语言=英语
  • example.com/webpage.php - > webpage.php?语言=英语
  • example.com/es,example.com/es/和example.com/es/index.php - >的index.php?语言=西班牙语
  • example.com/es/webpage.php - > webpage.php?语言=英语

当然,没有“es /”文件夹。一切都在内部完成。 还有一个类别页面,例如example.com/category.php?id=1 我更喜欢使用“beauty”链接,因此example.com/category/1/loremipsum.php必须在内部重定向到category.php?id = 1

我的目标是使类别与语言一起使用,即

  • example.com/category/1/loremipsum.php - > example.com/category.php?id=1&language=english
  • example.com/es/category/15/requiesqat.php - > example.com/category.php?id=15&language=spanish

我想也许不可能做到一切。我愿意混合php,cookies等。我很感激任何建议。

这是我的.htaccess:

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName example.com
Options -Indexes

RewriteEngine On

# 1. Force HTTPS and WWW
# Case 1: NO HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
# Case 2: NO WWW
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

# Module for languages
# This 2 blocks are for spanish: example.com/es
RewriteCond %{QUERY_STRING} (?!language) [NC]
RewriteCond %{REQUEST_URI} ^/es(/|/index.php)?$ [NC]
RewriteRule . index.php?language=spanish [S=3,NC,QSA]

RewriteCond %{QUERY_STRING} (?!language) [NC]
RewriteCond %{REQUEST_URI} ^/es(/)(?(1)(.*)?)$ [NC]
RewriteCond %{REQUEST_URI} !^/es/index\.php$ [NC]
RewriteRule ^es/(.*)?$ $1?language=spanish [S=2,NC,QSA]

# This 2 blocks are for english: example.com
RewriteCond %{QUERY_STRING} (?!language) [NC]
RewriteCond %{REQUEST_URI} ^(/|/index.php)?$ [NC]
RewriteCond %{REQUEST_URI} ^/(?!es|es/) [NC]
RewriteRule ^(.*)$ $1?language=english [S=1,NC,QSA]

# This is the problematic paragraph. If I left previous ones, website works properly,
# except for webpages other than "index.php". So I add this to deal with all the other
# pages and redirect to english site. It redirects internally but it does not change
# language. It seems that query string ?language=english is not good.
RewriteCond %{QUERY_STRING} (?!language) [NC]
RewriteCond %{REQUEST_URI} ^.*$ [NC]
RewriteCond %{REQUEST_URI} ^/(?!es|es/) [NC]
RewriteRule ^(.*)$ $1?language=english [NC,QSA]

# Categories
RewriteRule ^category/([0-9]+)/[a-zA-Z0-9_-]+\.php$ category.php?id=$1 [L,NC,QSA]

0 个答案:

没有答案