Man*_*nny 8 php .htaccess mod-rewrite
其实我有这个网址:
http://www.example.com/index.php?site=contact¶m1=value1¶m2=value2¶m3=value3
Run Code Online (Sandbox Code Playgroud)
但我希望有这种URL格式:
http://www.example.com/contact/param1:value1/param2:value2/param3:value3
Run Code Online (Sandbox Code Playgroud)
因此,"接触"变为可变$_GET["site"]的参数和休息应该能够通过访问$_GET["param1"],$_GET["param2"]等等的问题是,它与任意数量的参数来工作(有可能是param4甚至param50或参数的任何其他名称).是否有可能通过htaccess来涵盖所有这些案件?
小智 7
Mod_rewrite最多可以发送10个变量:
RewriteRule反向引用:这些是$ N(0 <= N <= 9)形式的反向引用,它提供对RewriteRule的模式的分组部分(括号中)的访问,RewriteRule受当前RewriteCond条件集的约束. mod_rewrite手册
所以你想要的只是htaccess不可能.一种常见的方法是将所有内容重写为一个文件,并让该文件以如下方式确定要执行的操作:
的.htaccess
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,NC]
Run Code Online (Sandbox Code Playgroud)
的index.php
$aUrlArray = explode('/',str_ireplace(',','/',$_SERVER['REQUEST_URI'])); // explode every part of url
foreach($aUrlArray as $sUrlPart){
$aUrlPart = explode(':',$sUrlPart); //explode on :
if (count($aUrlPart) == 2){ //if not 2 records, then it's not param:value
echo '<br/>paramname:' .$aUrlPart[0];
echo '<br/>paramvalue' .$aUrlPArt[1];
} else {
echo '<br/>'.$sUrlPart;
}
}
Run Code Online (Sandbox Code Playgroud)
使用一些创造性的 htaccess 和 PHP 是完全可行的。实际上,您在这里所做的就是告诉 Apache 将所有页面请求定向到 index.php(如果它们不是针对服务器上的真实文件或目录)...
## No directory listings
IndexIgnore *
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
## Mod_rewrite in use.
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Run Code Online (Sandbox Code Playgroud)
之后,您需要做的就是进入 PHP 并使用$_SERVER['REQUEST_URI']超全局访问完整的用户请求的 URL 结构,然后使用explode("/", $_SERVER['REQUEST_URI']).
我目前在许多网站上使用它,所有网站都由index.php 提供服务,但具有 url 结构,例如...
http://www.domain.com/forums/11824-some-topic-name/reply
然后由爆炸命令处理以出现在数组中...
0=>"forums", 1=>"11824-some-topic-name",2=>"reply"