簡體   English   中英

htaccess重寫規則,其中包含查詢字符串,多個變量且鏈接中沒有php文件

[英]htaccess rewrite rule with query string, multiple variables and no php file in the link

我們正在嘗試將論壇遷移到另一個平台,並且遇到了其中包含查詢的鏈接,例如

http://forum.test/threads/119312-Warnight-CS-GO?p = 2306618&viewfull = 1#post2306618

必須指向http://forum.test/threads/warnight-cs-go.119312/#post-2306618

因此,原始鏈接的邏輯結構是:

http://{forum_base_url}/threads/{thread-id}-{thread-permalink}?p={post-id}&viewfull={post-number-in-thread}#post{post-id}

新的是:

http://{forum_base_url}/threads/{thread-permalink}.{thread-id}/#post-{post-id}

因此,為了使重寫正常進行,我們需要從原始鏈接中“拉出”三件事:線程ID,永久鏈接和后ID。 前兩個不是問題,這是第三個不想合作的問題。

在Internet上尋找可能的解決方案后,我們想到了:

RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)#post(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,L]

但不幸的是,重寫無法正常進行。

與重寫有關的是,查詢中有多個變量,並且鏈接本身未指定.php文件,因此我們不能僅使用此處提供的解決方案: https : //stackoverflow.com/a/2252242 / 1288397

關於如何克服這一特殊障礙的任何技巧?

URL的#post- 永遠不會發送到服務器 ,它完全保留在瀏覽器的末尾,因此無法與之匹配。 幸運的是,帖子ID已經在查詢字符串中,因此您可以忽略它:

RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,L]

另一件事是,在第二個URL中,線程永久鏈接是小寫的。 不知道這是否重要,但是為了將文本更改為小寫,您需要使用Rewrite Maptolower內部函數。 您只能在服務器vhost / config中聲明地圖,因此,如果您無權訪問這些配置文件,則將無法使用地圖。

感謝您清理#post-方面。

我嘗試了您的修改,但是鏈接最初變成了:

http://forum.test/threads/warnight-cs-go.119312/%23post-2306618#post2306618

為了工作,需要重寫

RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,NE,L]

請注意,最后還有其他NE(noscape)標志

這是因為默認情況下,特殊字符(例如&和?)將轉換為等效的十六進制代碼。 使用[NE]標志可以防止這種情況的發生,如下所示: http : //httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM