簡體   English   中英

如何在.htaccess中編寫mod_rewrite規則

[英]how to write mod_rewrite rule in .htaccess

我正在使用htaccess文件中的mod_rewrite將一些URL重定向到特定頁面。 該項目是在核心PHP(無框架)中開發的,並且我正在xampp服務器上工作,

例如:我想將以下網址重定向到test.php

localhost/project/any_string //should be redirected to test.php
localhost/project/any_string/test //should be redirected to new_test.php

localhost/project/any_string.php //should be redirected to any_string.php only
localhost/project/any_string/any_string.php //should be redirected to any_string/any_string.php only

我啟用了mod_rewrite,任何幫助將不勝感激。

如果我了解您的需求,那么可以做到:

RewriteEngine On
RewriteRule ^(.*)project/(.*)/(.*).php$ /$2/$3.php [L,R=301]
RewriteRule ^(.*)project/(.*).php$ /$2.php [L,R=301]
RewriteRule ^(.*)project/(.*)test$ /new_test.php [L,R=301]
RewriteRule ^(.*)project/(.*)$ /test.php [L,R=301]

順便說一句,這里的規則順序非常重要,因此您必須使其完全相同。

您是否正在尋找類似以下的內容?

RewriteEngine On
RewriteRule localhost/project/(.*)/test new_test.php [R]
RewriteRule localhost/project/(.*) test.php [R]
RewriteRule localhost/project/(.*)/(.*).php $1/$2.php [R]
RewriteRule localhost/project/(.*).php $1.php [R]

[R]標志強制重定向。 請注意,較長的匹配項將在較短的匹配項之前列出(否則,較長的匹配項將永遠不會被匹配。)

暫無
暫無

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

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