.htaccess 具有多个变量的友好 URL [英] .htaccess for friendly URL with multiple variables

查看:33
本文介绍了.htaccess 具有多个变量的友好 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使我的网站对 SEO 更友好,我目前正在使用

I am working on making my site more SEO friendly I am currently using

RewriteEngine On
RewriteRule (.*)$ index.php?page=$1

site.co.uk/index.php?page=page_name 变成 site.co.uk/page_name

我也想将它用于子页面.我试过这个:

I want to also use this for sub pages too. I have tried this:

RewriteEngine On
RewriteRule (.*)/(.*)$ index.php?page=$1&subpage=$2

但它不工作,它运行 site.co.uk/page_name/sub_page 但是当你去 site.co.uk/page_name 它返回 404 not找到了.

but its not working, it runs site.co.uk/page_name/sub_page but when you go to site.co.uk/page_name it returns the 404 not found.

我希望它运行 site.co.uk/page_name 并返回 page_name

I want it to run site.co.uk/page_name and it return page_name

site.co.uk/page_name/sub_page 并返回 sub_page

等等

site.co.uk/page_name/sub_page/sub_page2/...

推荐答案

只使用:

RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

很好,然后您可以在脚本中路由请求.

is fine then you can route the request within your script.

你用explode()分隔符/分割页面变量,然后设置变量.

You split the page variable with explode() delimiter / and then set the variables.

$route = explode('/',$_GET['page']);

$page    = isset($route[0]) ? $route[0] : null;
$subpage = isset($route[1]) ? $route[1] : null;
$yada    = isset($route[2]) ? $route[2] : null;

很多MVC框架都使用这种方法.它叫做路由.

Many MVC frameworks use this method. Its called routing.

这篇关于.htaccess 具有多个变量的友好 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆