2011-06-07 116 views
0

我目前有PHP脚本可以正常工作,被调用如下: www.example.com/user.php/paul 和 www.example.com/ tag.php/food.htaccess mod_rewrite删除.php扩展名,但保留URL路径

我很难让.htaccess正确重写。我试图做到这一点: www.example.com/user/paul www.example.com/tag/food

到目前为止,我可以把它重定向/用户到/user.php,但/保罗丢失;打破我的剧本。

我目前的.htaccess看起来是这样的:

RewriteEngine On 
RewriteCond %{THE_REQUEST} ^GET\ /([^.\ ]+\.)+php(\?[^\ ]*)?\ HTTP 
RewriteRule ^(.+)\.php$ http://www.example.com/$1 [R=301,L] 
RewriteRule ^([^/]+/)*index/?$ http://www.example.com/$1 [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L] 
RewriteCond $1 !^([^.]+\.)+([a-z0-9]+)$ [NC] 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*[^/])$ /$1.php [L] 

请帮助。 谢谢! 保罗

回答

1

你也许应该使用的规则是这样的地方:

RewriteRule ^/([^/]*)/([^/]*)$ /$1.php/$2 

这里又是它的英文。

First comes the beginning of the path      ^/ 
The first component of the path doesn't have/symbols in it [^/]* 
We remember it            () 
Then comes the slash between the components    /
Then the second component without the/symbols    [^/]* 
Remember it too            () 
And the path ends           $ 

用第一个记住的东西代替它,然后是.php/,然后是第二个记住的东西。

您可能不希望在此规则中使用[L],重定向不是必需的,用户不需要知道您的脚本真的具有.php后缀。

希望这是正确的,我现在无法在工作的Apache上检查它。

+0

嗯,我不能得到这个工作,无论是 – Paul 2011-06-07 22:11:57

+0

任何其他的想法? – Paul 2011-06-08 20:57:18

0

将这个代码在.htaccess文件:

Options +FollowSymlinks -MultiViews 
RewriteEngine on 

# external redirect to hide .php 
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC] 
RewriteRule ^([^/]+)\.php/(.*)$ /$1/$2 [NE,R=301,L,NC] 

# internal redirect to add .php 
RewriteRule ^([^/]+)(?<!\.php)/(.*)$ /$1.php/$2 [L] 
+0

对不起,这对我无效 – Paul 2011-06-08 05:09:29

+0

@Paul:可能你可以提供一些不起作用的细节。顺便说一句,我已经在发布之前在我的Linux和Mac上测试了这个答案。 – anubhava 2011-06-08 13:19:00

+0

问题和以前一样:/ user正确地重定向到/user.php,但/ user/paul给出了一个404 – Paul 2011-06-09 00:44:57