问题
I'm running a wiki and the URLs look like this:
/wiki/index.php?title=MyTitle
But I'd like it to look like this:
/wiki/MyTitle
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]
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.
回答1:
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
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.
You can access the parameter from the PHP by making an array (again, from the linked site):
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
Then $requestURI
will be an array of the parameters given, so this:
/wiki/MyTitle
would be turned into:
Array ( [0] => [1] => wiki [2] => MyTitle )
来源:https://stackoverflow.com/questions/11303643/rewrite-url-parameters-as-directories-like-a-wikipedia-url