doumowu7371 2017-10-30 09:13
浏览 209
已采纳

使用PHP -S localhost服务器时如何获得重定向工作(.htaccess)?

I used xampp in the past in order to develop websites on a local computer, but I recently found it less cumbersome to simply start a server using the PHP -S localserver:8000 where 8000 is the port number. I managed to get phpmyadmin working that way and access databases. So all was fine ... until I tried to re-use redirecting rules within a .htaccess file.

The rules work well on the my web provider's servers, but it doesn't work on localhost. I assume there's something to do in order to turn the re-write rules on but since I am not longer using xampp, I don't know if there's still something like Apache running in the background, etc. If someone has experience with this setup, I would like to know how or if this can be be done.

EDIT

The redirect is basic:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule lessons/(.*)$ page.php?url=$1

And to make the question more meaningful (addressing some of the comments below), I think Finwe answered my question rightly, that is: "this doesn't work with the PHP approach, .htaccess is specific to Apache".

My question wasn't about "what alternative solution do I have". The solution I would use is probably to go through a combination of AJAX/PHP to solve the limitation but that's surely not the most straightforward workaround.

  • 写回答

2条回答 默认 最新

  • doufa5001 2017-10-30 09:16
    关注

    PHP's internal server does not recognize .htaccess files. That is a privilege of using Apache server.

    The redirects must be a part of your PHP application to retain them across all platforms.

    With PHP internal server, you can run it with a PHP file parameter.

    If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser.

    PHP -S localserver:8000 router.php
    

    The script could then replace the htaccess file checking and "routing" requests to lessons/.

    For more complex applications I would suggest using a "real" webserver such as Apache or nginx as the built-in server has more limitations, such as hanging on external HTTP requests, as it is only single threaded.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云