2011-06-07 163 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