简体   繁体   English

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

[英]How do I configure Apache (.htaccess?) to use mod_rewrite to pass virtual path to PHP file?

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. 我有一个位于http://www.foo.bar/script的脚本,并希望使用虚拟路径名将数据发送到该脚本。 For example, http://www.foo.bar/script/this/is/a/path would pass "this/is/a/path" to /script. 例如, http://www.foo.bar/script/this/is/a/path会将“this / is / a / path”传递给/ script。 I would like to do this without changing the URL the user sees. 我想这样做而不改变用户看到的URL。

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 . 我已经使用Apache mod_rewrite使用类似于此处建议的内容htaccess mod_rewrite查询字符串的多个路径 What I have not been able to do is to pass the path to the script without changing the URL the users sees. 我无法做的是将路径传递给脚本而不更改用户看到的URL。 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. 因此,访问http://www.foo.bar/script/stackoverflow/rocks的用户,驻留在/ script的脚本将接收/ stackoverflow / rocks作为查询字符串或URI,但URL不会更改。 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. 但是,以上更改了浏览器中的URL。 I don't want to expose "?" 我不想公开“?” on the query string. 在查询字符串上。

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. 上面使用rewrite_module的示例将使用.php重写任何URL,并且还将接受没有.php的页面名称,并且重定向到pageName.php,这个名称接近您想要的名称,用户编写的名称,重定向到您想要的脚本。 Additionaly it also offer some protection against cross scripting at the end of the URL. 另外,它还在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM