Apache的重写问题,自定义的URI [英] Apache Rewrite Issue, Custom URI

查看:166
本文介绍了Apache的重写问题,自定义的URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解决方案,但它是一个我知道是不是最大的,更好地要做一个完整的重写来解决。基本上我有3重写,将去我想,做他们需要做的正确的地方。不过,为了在那里我需要去之间切换,我不得不写一个URI类脱衣通过URL设置页面并手动瓦尔。它们都工作地很好,但网址已经一个痛苦的屁股特别是如果不完全格式化。

I have a solution but it is one that I know is not the greatest and would better be solved with a full rewrite. Basically I have 3 rewrites that will go to the correct areas I want and do what they need to do. However in order to switch between where I need to go I had to write a URI class to strip through the url set the page and vars manually. It all works out great but the urls are a pain in the ass specially if not formatted exactly.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/bsiadmin/$ /bsiadmin/index.php [L,QSA]
RewriteRule ^/bsiadmin/(.+)/$ /bsiadmin/index.php?page=$1 [L,QSA]
RewriteRule ^/(.+)/$ /index.php?page=$1 [QSA]

因此​​,第一条规则将确保指挥一切的目录,而不是根目录的index.php,第二条规则不相同的,如果有一个页指定。最后一条规则将采取别的,并确保它使用根index.php文件,并从那里去。

So the first rule will make sure to direct everything to the directory and not the root index.php, the second rule does the same if there is a "page" specified. The last rule will take anything else and make sure it uses the root index.php and goes from there.

网址,例如:

http://mysite.test/icecream/id=2/

我的自定义的URI类会剥夺这种清洁,并设置ID为$ _REQUEST变种。

My custom uri class would strip this clean and set id as a $_REQUEST var.

我想我真正想知道的是我如何才能重写一个简单的URL,例如:

I guess what I really want to know is how can I just rewrite a simple url such as:

http://mysite.test?page=icecream&id=2

http://mysite.test/icecream/id/2/

没有多少增值经销商可以传递任何限制,而且确实存在bsiadmin,显示无我不必使用URI类直接吧。目录

Without any limitation on how many vars can be passed and the directory that does exist "bsiadmin" to display without me having to use a uri class to direct it.

感谢您的帮助。

推荐答案

您可以使用mod_rewrite这样做的:

You can use mod_rewrite to do so:

RewriteRule ^/([^/]+)/([^/]+)/([^/]+)/(.*) /$1/$4?$2=$3 [N,QSA]
RewriteRule ^/([^/]+)/$ /bsiadmin/index.php?page=$1 [L,QSA]

但我认为最好是使用PHP该作业。因为mod_rewrite的,你只能在一个时间(在这里,人们每改写)改写URL参数的固定金额。用PHP可以解析参数任意数量是这样的:

But I think the best would be to use PHP for that job. Because with mod_rewrite you can only rewrite a fixed amount of URL arguments at a time (here one with every rewrite). With PHP you can parse any arbitrary number of arguments like this:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);
if (count($segments) > 2) {
    for ($i=4, $n=count($segments); $i<$n; $i+=2) {
        $_GET[rawurledecode($segments[$i-1])] = rawurldecode($segments[$i]);
    }
    $_GET['page'] = rawurldecode($segments[1]);
}

然后,所有你需要的是mod_rewrite的这个单一规则的要求重写你的的index.php 的:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^/bsiadmin/index\.php$ /bsiadmin/index.php [L]

这篇关于Apache的重写问题,自定义的URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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