2011-11-12 101 views
1

我的.htaccess文件中有一個RedirectMatch規則可以正常工作,但我遇到了一個使用mod_rewrite的類似規則。mod_rewrite相當於一個RedirectMatch規則

我的目標是擁有此URL:mysite.com/anything/print顯示此頁:mysite.com/anything?view=print。

,致力於重定向它的規則是這樣的:

RedirectMatch 301 ^(.*)/print/?$ http://mysite.com/$1?view=print 

但現在我想從一個可見的301改變這種重定向到一個「看不見」的使用mod_rewrite重寫。我已經嘗試了許多不同的變化對這個(有和沒有RewriteBase),並沒有工作過:

RewriteEngine On 
RewriteBase/
RewriteRule ^(.*)/print/? $1?view=print 

我在做什麼錯? Mod_rewrite是肯定啓用的,並且在同一個.htaccess文件中有基於Wordpress的mod_rewrite規則。


UPDATE

從@Nathan使用技巧,我現在都沒有了。但是,當我訪問mypost/print時,仍然收到404。

RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_URI} !^/index\.php 
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [L] 

當我附加/打印/到一個固定鏈接,所述WP_DEBUG插件指示如下內容:

請求:myposttype/mypost/print

查詢字符串:attachment=print

匹配重寫規則:myposttype/[^/]+/([^/]+)/?$

匹配重寫查詢:attachment=print

+0

你有其他規則嗎? –

回答

1
RewriteEngine on 
RewriteRule ^(.*)/print/$ /$1?view=print [L] 

RewriteRule ^(.*)/print$ /$1?view=print [L] 

沒有 「/」 的URL字符串的結尾

1

如果你在htaccess文件典型WordPress的規則,你的規則應該在# BEGIN WordPress塊之前。否則,在調用規則之前,wordpress規則將停止重寫匹配。

此外,你應該你的正則表達式之後添加$除非你也想匹配的是這樣的:http://domain.com/page/print/something/else/here

最後,爲了讓WordPress識別URL中的變化,而不會重定向頁面,則需要追加到index.php/的URL,以便Wordpress將使用路徑信息永久鏈接。

E.g.

RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_URI} !^/index\.php 
RewriteRule ^(.*)/print/?$ /index.php/$1?view=print [L] 

# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

# END WordPress 
+0

這看起來應該可以工作,但當我轉到domain.com/mypost/print時,仍然會收到404。使用WP Debug插件,我發現查詢正在查找attachment = print(帶有'print'的插件的媒體附件)。你能想到解決這個問題的方法嗎? – supertrue

+0

WordPress將使用原始URL來解析永久鏈接,除非您將重定向更改爲使用路徑信息永久鏈接,如index.php/url/path/here?view = print。我已更新示例以包含路徑信息方法。 – Nathan

+0

非常感謝您的更新。更新後的代碼正在發生同樣的404和attachment = print查詢;沖洗永久鏈接沒有幫助。手動插入index.php(如mysite.com/index.php/mypost/print)也不起作用,並給出相同的404。任何想法? – supertrue