我正在尝试将使用ID作为参数的有效的.htaccess
代码更改为一个名称,以便可以使用更友好的URL。
例如,我想拥有
myurl.com/pagina/页面名称
代替
myurl.com/pagina/1
这是工作代码
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
# redirect "/pagina.php?id=xxx" to "/pagina/xxx"
RewriteCond %{THE_REQUEST} pagina\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /pagina/%1? [R=301,L]
# internally rewrite "/pagina/xxx" to "/pagina.php?id=xxx"
RewriteRule ^pagina/([0-9]+)$ actagov/pagina.php?id=$1 [L]
我试图将其更改为
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
# redirect "/pagina.php?alias=xxx" to "/pagina/xxx"
RewriteCond %{THE_REQUEST} pagina\.php\?alias=([^&\s]+)\s [NC]
RewriteRule ^ /pagina/%1? [R=301,L]
# internally rewrite "/pagina/xxx" to "/pagina.php?alias=xxx"
RewriteRule ^pagina/([^&\s]+)$ actagov/pagina.php?alias=$1 [L]
PHP文件
if(isset($_GET['alias']))
{
if(!empty($_GET['alias']))
{
$alias_pagina = $_GET['alias'];
$result_pagina = mysqli_query($mysqli, "SELECT * FROM pages WHERE
nome='$alias_pagina'");
$query_pagina = mysqli_fetch_assoc($result_pagina);
$id_pagina = $query_pagina['id'];
$id_template = $query_pagina['id_template'];
switch($id_template)
{
case '1':
include 'page_template_standard.php';
break;
case '2':
include 'page_template_two_column.php';
break;
}
}
else
{
header('Location: index.php');
}
}
else
{
header('Location: index.php');
}
?>
但是它根本没有重定向。我可能会误解语法,但是对 htaccess 语言不是很熟悉。你能告诉我我应该改变什么吗?
编辑:我终于找到了可行的解决方案
Options -MultiViews
RewriteEngine On
# redirect "pagina.php?alias=xxx" to "/xxx"
RewriteCond %{THE_REQUEST} /pagina\.php\?alias=([a-zA-Z0-9_-]+) [NC]
RewriteRule ^ /actagov/%1? [R=301,L]
# internally rewrite "/xxx" to "pagina.php?alias=xxx"
RewriteRule ^([a-zA-Z0-9_-]+)$ pagina.php?alias=$1 [L]
这将转换网址
localhost / actagov / pagina.php?alias = alias-name
进入
localhost / actagov / alias-name