php - 使用 .htaccess 重写多个 URL

标签 php apache .htaccess url mod-rewrite

我需要一些帮助来为我的网站重写不同的 URL。这是一个多语言网站,因此有点复杂。我已经设法让一些事情发挥作用。这就是我的代码到目前为止所做的事情:

  • 定义 RewriteBase(因为本地主机和生产服务器具有不同的基数)
  • 将 www.mysite.com 重写为 www.mysite.com/en/(或其他语言,具体取决于“lang”参数)
  • 将非 www 重定向到 www
  • 从网址中删除 index.php

我想要什么

但是我的代码有两个问题:

  • 它重定向到 http://www.example.com/en/?lang=en 。但我不想要最后一个参数,而且我无法用我的基本技能弄清楚如何隐藏“lang”。如何防止将该参数添加到 URL 中?
  • 当我去,比方说,http://www.example.com/?page=news/en/不会添加到 URL 中。如何解决这个问题?它应该始终在每个网址中在域后面添加语言。

除此之外,我想要两个额外的重写规则,但我不希望我的 htaccess 变得一团糟。所以我希望有人能指出我正确的方向。在我的 htaccess 中执行此操作的最佳方法是什么?

对于所有这些规则:URL 应该以/结尾,如果不是,则应该添加它:所以 http://www.example.com/en/news -> http://www.example.com/en/news/ .

这是我现在的代码:

Options +FollowSymlinks 
RewriteEngine On
RewriteBase /

# SET REWRITEBASE VARIABLE
RewriteCond %{HTTP_HOST} !=localhost
RewriteRule ^ - [E=FRB:/]
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^ - [E=FRB:/www.example.com/]


## REWRITE FOR LANGUAGE ##
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ %{ENV:FRB}en/ [R=301,L]

# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|nl)$  %{ENV:FRB}$1/ [R=301,L]

# now all urls have en/ de/ -> parse them
RewriteRule ^(en|nl)/(.*)$  %{ENV:FRB}$2?lang=$1&%{query_STRING} [L]



## REWRITE NON-WWW TO WWW (EXCEPT ON LOCALHOST) ##
RewriteEngine On
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]



## REMOVE INDEX.PHP FROM URL ##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

非常感谢您对我的帮助!如果您有疑问,请告诉我。

最佳答案

像这样:

Options +FollowSymlinks 
RewriteEngine On

# SET REWRITEBASE VARIABLE
RewriteCond %{HTTP_HOST} !=localhost
RewriteRule ^ - [E=FRB:/]

RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^ - [E=FRB:/www.example.com/]

## REWRITE FOR LANGUAGE ##
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ %{ENV:FRB}en/ [R=301,L]

## REMOVE INDEX.PHP FROM URL ##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]

## REWRITE NON-WWW TO WWW (EXCEPT ON LOCALHOST) ##
RewriteCond %{HTTP_HOST} !(^www\.|localhost) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]

# handle /en/news/2452/lorem-ipsum/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*[a-z]{2})/?\?page=([^\s&]+)&id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/? [R=302,L,NE]

RewriteRule ^(en|nl)/(news)/(\d+)/([\w-]+)/?$ index.php?lang=$1&page=$2&id=$3&news=$4 [L,QSA]

# handle /en/info
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*[a-z]{2})/?\?page=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [R=302,L,NE]

RewriteRule ^(en|nl)/([\w-]+)/?$ index.php?lang=$1&page=$2 [L,QSA]

# now all urls have en/ OR de/ -> parse them
RewriteRule ^(en|nl)/(.*)$  %{ENV:FRB}$2?lang=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?$1 [L,QSA]

关于php - 使用 .htaccess 重写多个 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36721228/

相关文章:

.htaccess - 在我的重写和规则 .htaccess 文件上强制使用 HTTPS

Apache Tomcat 重写

php - 通过 PHP 在电子邮件中发送 HTML 页面

php - 在 PHP 中转义 elasticsearch 特殊字符

apache - 如何在 Docker 上运行的 Apache 服务器中安装 mod_auth_openidc 模块

ruby-on-rails - mod_passenger "Cannot connect to Unix socket"错误

php - SQL : Syntax error or access violation

php - 在多个 html 文件中包含相同的页眉和页脚

Apache 配置读取 header 值

php - 运行 PHP cronjob 超过 15 分钟