Apache presenting virtual subdirectories and passing parameters to PHP script

安稳与你 提交于 2019-12-25 04:10:05

问题


My root directory contains a selection of PHP files: index.php, about.php etc. Each of these can take an ID parameter to customize a few of the variables on the page, such as contact phone number, email address etc.

If a visitor was to access http://myserver/joebloggs (joebloggs being a parameter, not a subdirectory), I want them to be served up with the index.php page from the root directory, but for joebloggs to be passed to index.php as the ID and the page then customized. I don't want to have to create a subdirectory for every user ID, I'd rather maintain this info in a DB and have PHP generate the pages for me.

Furthermore, when the user then navigates to the about.php page, I would like this ID to be carried over and then have the about page customized too, i.e. a link to 'about' maintains the ID parameter in the URL: http://myserver/joebloggs/about/

I've got a rough idea in my mind how to do this and have been reading up on mod_rewrite but haven't had much luck in piecing various solutions together.

Any pointers or help would be much appreciated.


回答1:


Try something like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/(.+?)\/about
RewriteRule . /about.php?p1=%1 [L]

RewriteCond %{REQUEST_URI} \/.*?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /index.php?p1=$1 [L]

Should do the job. Tested it on my server.



来源:https://stackoverflow.com/questions/10362349/apache-presenting-virtual-subdirectories-and-passing-parameters-to-php-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!