Htaccess用虚拟目录和2个变量重写URL

时间:2015-06-14 20:25:45

标签: regex apache .htaccess url rewriting

我的网址......

result.php?team=arsenal&player=ospina

我想要这样

mysite.com/virtualdirectory/arsenal/ospina.html

我试过这段代码..无法工作..找不到

RewriteRule ^(.*)/(.*)/(.*).html$ result.php?team=$2&player=$3 [L]

在此服务器上找不到请求的网址/subfolder/arsenal/ospina.html。

本地主机端口80上的Apache / 2.2.8(Win32)PHP / 5.2.6服务器

感谢您的帮助, 最好的问候!!

2 个答案:

答案 0 :(得分:0)

在.htaccess文件中

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.*]+)/([^\.*]+)/([^\.*]+).html$ result.php?team=$2&player=$3 [L,QSA]

答案 1 :(得分:0)

好的,我假设您要从

更改URI
 http://www.example.com/result.php?team=arsenal&player=ospina

 http://www.example/subdirectory/arsenal/ospina.html

所以这是为你做的那个.htaccess

RewriteEngine on

RewriteCond %{QUERY_STRING} ^team=(.*)\&player=(.*)$ 
RewriteRule ^(.*)$ http://www.example.com/subdirectory/%1/%2.html [R=301]

你可以在这里用htaccess测试仪进行测试 http://htaccess.madewithlove.be/

以及一些有用的文档和学习链接:

http://httpd.apache.org/docs/2.2/rewrite/intro.html

http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708

Great Video Tutorials - Understanding Apache and htaccess

希望有所帮助