dragon4808 2019-06-10 13:32
浏览 65

如何配置Apache(.htaccess?)使用mod_rewrite将虚拟路径传递给PHP文件?

I have a script located at http://www.foo.bar/script and would like to use virtual path names to send data to that script. For example, http://www.foo.bar/script/this/is/a/path would pass "this/is/a/path" to /script. I would like to do this without changing the URL the user sees.

I've already gotten this to work with Apache mod_rewrite using something similar to what was suggested here htaccess mod_rewrite multiple paths to query string. What I have not been able to do is to pass the path to the script without changing the URL the users sees. So, a user that visits http://www.foo.bar/script/stackoverflow/rocks, the script residing at /script would receive /stackoverflow/rocks as a query string or URI but the URL would not change. I know this is not uncommon, and perhaps I'm using the wrong terminology when searching for an answer. Thank you for considering.

One idea someone suggested:

RewriteEngine On
RewriteBase /
RewriteRule "script/(.*)" "https://www.foo.bar/script/?data=$1" [R,L]

However, the above changes the URL in the browser. I don't want to expose "?" on the query string.

  • 写回答

1条回答 默认 最新

  • dtcu5885 2019-06-10 13:46
    关注

    I don't really have the exact code you need but I have something I use that might just help you. The example above using rewrite_module will rewrite any URL with .php and will also accept the page name without the .php and redirect to a pageName.php witch is close to what you want, the name the users write, redirecting to the script you want. Additionaly it also offer some protection against cross scripting at the end of the URL.

    <IfModule rewrite_module>
       RewriteEngine On
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule ^([^\.]+)$ $1.php [NC,L]
       RewriteCond %{QUERY_STRING} ^(.*?)(?:%3C|%3E|&lt;|&gt;)(.*)$
       RewriteRule ^((.*)((?!([.]*p*h*p))\w+)) /$1?%1%2 [L,R=302,NE]
    </IfModule>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算