mod_rewrite / foo到index.php / foo和/ foo / foo2到index.php / foo / foo2

时间:2013-09-01 15:52:46

标签: php mod-rewrite url-rewriting rewrite

我想重写

http://www.example.com/foo

http://www.example.com/index.php/foo

internaly。

但uris喜欢

http://www.example.com/foo/foo2

也应该有效!

我在mod_rewrite中并不安静,所以请帮帮我:)。

1 个答案:

答案 0 :(得分:0)

试试这个。

.htaccess

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

如果这是index.php

<?php

var_dump($_SERVER['SCRIPT_URL']);

访问"http://www.example.com/foo",结果将是:

string(4) "/foo"

"http://www.example.com/foo/foo2"将是:

string(9) "/foo/foo2"