的.htaccess为友好的URL多变量 [英] .htaccess for friendly URL with multiple variables
问题描述
我的工作让我的网站更多的搜索引擎友好的,我目前使用
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未找到。
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
或
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.
您分裂与爆炸(页面变量)分隔/,然后设置变量。
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屋!