我需要一些帮助来为我的网站重写不同的URL。这是一个多语言站点,因此使其有些复杂。我设法使某些事情起作用。到目前为止,这是我的代码执行的操作:
但是我的代码有两个问题:
除此之外,我还想再添加两个重写器,但我不希望htaccess变得一团糟。所以我希望有人能指出我正确的方向。在htaccess中执行此操作的最佳方法是什么?
参数“页面”包含正在显示并应重写的页面:http : //www.example.com/en/index.php? page = info- > http://www.example.com/en/info /(我已经弄清楚了如何隐藏index.php,请参见下面的代码)
我的新闻页面(http://www.example.com/en/index.php?page=news&id=2452&news=lorem-ipsum)应该重写为http://www.example.com/en/news/2452/ lorem-ipsum /。
对于所有这些规则: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]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句