我需要Apache的mod_rewrite规则的帮助。
我刚开始使用PHP和.htaccess
。我被困在尝试获取用户的查询URL字符串,解析"语言"和"页面"作为GET变量,将它们传递给root中的index.php
文件,并通过编译的网页响应用户。另外,当我没有设置用户URL中的一个或两个参数时,我需要我的脚本正确理解默认语言(" ee")和页面("索引")值。示例:
1) "example.com/" --> "example.com/index.php?page=index&lang=ee"
2) "example.com/se/samples" --> "example.com/index.php?page=samples&lang=se"
3) "example.com/se/" --> "example.com/index.php?page=index&lang=se"
4) "example.com/contact" --> "example.com/index.php?page=contact&lang=ee"
5) "example.com/ru/contact" --> "example.com/index.php?page=contact&lang=ru"
我需要Apache始终显示"清除网址"只有,如:example.com/se/samples
。 "丑陋的网址"使用GET查询仅出于技术原因,不应对用户和/或搜索引擎可见。我也关注安全问题,因此我很乐意使用.htaccess
和/或index.php
中手动设置的参数的白名单。
我在服务器端有以下网站文件结构:
www:
| .htaccess
| __php__.php
| index.php <-- controller
|
+---css [...]
|
+---img [...]
|
+---js [...]
|
+---lang <-- arrays of languages
| lang_ee.json
| lang_en.json
| lang_ru.json
| lang_se.json
| sliders.json
|
\---tmpl <-- layout templates of pages
contact.php
footer.php
header.php
index.php
nav.php
retail.php
samples.php
services.php
我需要在前端支持以下站点地图结构:
lang=ee page=index example.com/
lang=ee page=services example.com/services
lang=ee page=retail example.com/retail
lang=ee page=samples example.com/samples
lang=ee page=contact example.com/contact
lang=en page=index example.com/en/
lang=en page=services example.com/en/services
lang=en page=retail example.com/en/retail
lang=en page=samples example.com/en/samples
lang=en page=contact example.com/en/contact
lang=ru page=index example.com/ru/
lang=ru page=services example.com/ru/services
lang=ru page=retail example.com/ru/retail
lang=ru page=samples example.com/ru/samples
lang=ru page=contact example.com/ru/contact
lang=se page=index example.com/se/
lang=se page=services example.com/se/services
lang=se page=retail example.com/se/retail
lang=se page=samples example.com/se/samples
lang=se page=contact example.com/se/contact
我当前的index.php
:
<?php
if (isset($_GET['page'])) {
$page=$_GET['page'];
} else {
$page='index';
}
if (isset($_GET['lang'])) {
$lang=$_GET['lang'];
}
else { $lang='ee'; }
// Include configs
include('__php__.php');
// Assign template
include (ROOT_DIR.'tmpl/'.$page.'.php');
?>
我当前的.htaccess
包含几个不幸的尝试:
# URLs rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#RewriteRule ^(services|retail|samples|contact)/?$ $1.php [L]
#RewriteRule ^(index|services|retail|samples|contact)$ index.php?page=$1&lang=ee [L]
#RewriteRule ^([^/].*)([^/].*)$ index.php?page=$2&lang=$1 [L]
#RewriteRule ^(en|ru|se)/(index|services|retail|samples|contact)$ index.php?page=$2&lang=$1 [L]
#RewriteRule ^([^(en|ru|se)].*)/$ index.php?page=$1&lang=ee [N]
#RewriteRule ^(.*)/(.*)$ index.php?page=$2&lang=$1 [L]
#RewriteRule ^(en|ru|se)/(services|retail|samples|contact)/?$ $1/$2.php [L]
#FFFFFUUU..
我已设法使其与一个或两个参数集合起作用(例如example.com/se/
或example.com/se/contact
),但我不知道在哪里进一步挖掘以使脚本具有通用性。原谅我生锈的英语。
修改
我只需要我的脚本来&#34;听&#34;以下语言:en
/ ru
/ se
和ee
(如果未设置);页面:services
,retail
,samples
,contact
和index
(如果未设置)。我仍然需要404
页面,例如example.com/blabla
。这就是我所说的&#34;预设参数&#34;问题标题。
答案 0 :(得分:0)
这可能有效:
RewriteEngine on
RewriteRule ^([A-Za-z]{2}+)/?$ /index.php?page=index&lang=$1 [R=301,NC,L]
RewriteRule ^([A-Za-z]{2}+)/(.*)$ /index.php?page=$2&lang=$1 [R=301,NC,L]
RewriteRule ^$ /index.php?page=index&lang=ee [R=301,NC,L]
RewriteRule ^((?!index.php).*)$ /index.php?page=$1&lang=ee [R=301,NC,L]
编辑:
RewriteRule ^$ /index.php?page=index&lang=ee [NC,L]
RewriteRule ^(services|retail|samples|contact)/?$ /index.php?page=$1&lang=ee [NC,L]
RewriteRule ^(en|ru|se)/?$ /index.php?page=index&lang=$1 [NC,L]
RewriteRule ^(en|ru|se)/(services|retail|samples|contact)?$ /index.php?page=$2&lang=$1 [NC,L]