在URL的mod_rewrite使用变量的数字位置 [英] mod_rewrite use variable's numeric position in url

查看:73
本文介绍了在URL的mod_rewrite使用变量的数字位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,如果标题是不是很清楚,因为我是新来mod_rewrite的,我不知道如何表达出该问题的正确。

I apologize if the title isn't quite clear as I am new to mod_rewrite and I am not sure how to phrase the question correctly.

我想重写一个奇特的URL,例如 http://www.url.com/module/subdirectory/another_subdirectory/page http://www.url.com/d_1=module&d_2=subdirectory&d_3=another_subdirectory&page=page.子目录的数目而异,以便参数D_ *可以是动态的(试图保持它D_1和D_9之间虽然)。花式网址也可以有自己的查询串与它需要重写的参数页面之后发生。

I want to rewrite a fancy url such as http://www.url.com/module/subdirectory/another_subdirectory/page to http://www.url.com/d_1=module&d_2=subdirectory&d_3=another_subdirectory&page=page. The number of subdirectories varies so the parameter d_* can be dynamic (trying to keep it between d_1 and d_9 though). The fancy url can also have its own query string and it needs to be rewritten to occur after the page parameter.

我通过Apache的mod_rewrite和正则表达式的文件做了读,花了一些时间来寻找对谷歌和#1的答案,这一点,但找不到任何为这个特定的问题。从别人更见地这个问题的任何帮助将大大AP preciated。

I did a read through of Apache's mod_rewrite and regex documentation and spent some time searching on Google and Stackoverflow for an answer to this but could not find anything for this specific problem. Any help on this problem from someone much more knowledgable would be greatly appreciated.

推荐答案

变更的乌尔里希的code,这并不需要尽可能多的重复,通过使子目录零部件可选。最后一部分始终映射到

Alteration of Ulrich's code that doesn't need as much repeating, by making the subdir-parts optional. The last part always maps to page:

RewriteRule ^(module)/(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?([^/]+)$ index.php?d_1=$1&d_2=$2&d_3=$3&d_4=$4&d_5=$5&d_6=$6&d_7=$7&d_8=$8&page=$9 [QSA,L,NC]

虽然我会preFER使用一些PHP code这一点。

Though I would prefer to use some php code for this.

$dirs = expode('/', $_SERVER['REQUEST_URI']);
array_shift($dirs);//remove first empty string.
$page = array_pop($dirs);//get last part of the url.

这篇关于在URL的mod_rewrite使用变量的数字位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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