2013-10-12 72 views
1

我想重寫這些URL一樣爲什麼我的重寫規則不能重寫?

site.com/eng?sentence=a-sentence 

是這樣的:

site.com/eng/a-sentence 

我只想在布勞爾URL改變,我不想重定向顯示的文字。

RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC] 
RewriteRule^/eng/%1 [NC,L,NE] 

我已經擺弄它,比較其他重寫規則,我發現很長一段時間,但都無法得到它的工作,我實在想不通爲什麼。每次我測試時,我都嘗試從我的htaccess中刪除所有其他重寫。

這裏是我的htaccess文件:

# Use PHP5.3 Single php.ini as default 
AddHandler application/x-httpd-php54s .php 
AddType application/x-httpd-php .htm .html 

Options -Indexes -MultiViews 

ErrorDocument 404 /404.php 
ErrorDocument 500 /500.php 

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L] 

RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC] 
RewriteRule^/eng/%1 [NC,L,NE] 


<FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> 
    Header set Cache-Control "max-age=604800, public" 
</FilesMatch> 

最好,如果你能解決我的規則,而不是寫自己的一個,我不明白可能,這將是巨大的。

回答

0

我只希望顯示在瀏覽器url中的文本發生變化,我不想要 重定向。

要在瀏覽器中更改URL,您需要使用R標誌的外部重定向。

?是特殊的正則表達式符號和匹配一個文字?你需要像\?轉義它。你也需要在目標URI的末尾有一個?去掉查詢字符串。

你完整的.htaccess:

RewriteEngine On 

# actual oneto pretty URL - external redirect 
RewriteCond %{THE_REQUEST} \s/+eng\.php\?sentence=([^&\s]+) [NC] 
RewriteRule^/eng/%1? [L,NE,R=302] 

# pretty URL to actual one - internal rewrite 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^eng/([^/]+)/?$ /eng.php?sentence=$1 [L,QSA,NC] 

# To internally forward /dir/foo to /dir/foo.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+?)/?$ /$1.php [L] 

我強烈建議您閱讀:Apache mod_rewrite Introduction

+0

我很想相信你,但該規則給了我一個500錯誤。即使我從我的htaccess中刪除所有其他重寫規則。 – user1397417

+0

那麼隔離的規則絕對有效並重定向到'/ localhost/eng/a-sentence'。但是,責任在於清理你的系統,並確保沒有其他.htaccess任何地方沒有其他重寫規則。報告500是不夠的,你需要閱讀你的apache error.log並找出真正的錯誤信息。 – anubhava

+0

現在檢查編輯完成.htaccess。請停止創建新問題,並參加此最終決議。 – anubhava