将 URL 参数重写为目录 - 就像维基百科 URL [英] Rewrite URL parameters as directories - like a Wikipedia URL
问题描述
我正在运行一个 wiki,网址如下所示:
I'm running a wiki and the URLs look like this:
/wiki/index.php?title=MyTitle
/wiki/index.php?title=MyTitle
但我希望它看起来像这样:
But I'd like it to look like this:
/wiki/MyTitle
/wiki/MyTitle
我该怎么做?我认为这可以通过 .htaccess 和重写规则来完成,我在这个网站和谷歌上环顾四周,但我找不到任何真正帮助我的东西.现在我在/wiki/目录的 .htaccess 中得到了这个.
How can I do this? I assume this can be done with .htaccess and rewrite rules, and I've looked around on this site and on google but I can't find anything that's really helping me. Right now I've got this, in the .htaccess for the /wiki/ directory.
RewriteEngine on
RewriteOptions MaxRedirects=1
RewriteRule ^(.+) index\.php\?title=$1 [L]
但这只是将我重定向到/wiki/index.php?title=Index.php 并抛出检测到重定向循环!"错误.我不确定我是否真的了解这些东西是如何工作的.
But that just redirects me to /wiki/index.php?title=Index.php and throws a "Redirect loop detected!" error. I'm not sure I really understand how this stuff even works.
推荐答案
与其制定复杂的规则,只需将所有流量路由到 index.php,然后将子目录"作为参数处理.我使用本指南来帮助我做所以.来自该网站:
Instead of making complicated rules, just route all the traffic to index.php, then handle the "sub-directories" as parameters. I used this guide to help me do so. From that site:
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
把它放在你的 .htaccess 文件中,所有的流量都会转到 index.php.然后编写 index.php 本质上是一个路由器",它将在您的服务器上加载和显示其他 PHP 脚本.
Put that in your .htaccess file, and the all the traffic will go to index.php. Then write index.php to be essentially a "router" that will load and show other PHP scripts on your server.
您可以通过创建数组(同样来自链接站点)从 PHP 访问参数:
You can access the parameter from the PHP by making an array (again, from the linked site):
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
然后 $requestURI
将是给定参数的数组,所以:
Then $requestURI
will be an array of the parameters given, so this:
/wiki/MyTitle
会变成:
Array ( [0] => [1] => wiki [2] => MyTitle )
这篇关于将 URL 参数重写为目录 - 就像维基百科 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!