簡體   English   中英

如何使用.htaccess刪除部分URl?

[英]How to remove part of URl with .htaccess?

目前,在我的網站上,我有20多個URL

http://www.example.net/content/index/mission

我想從所有網址中刪除/ content / index,所以它們應該看起來像這樣

http://www.example.net/mission

換句話說,我將始終從URL中刪除/ content / index。 我敢肯定,這確實很簡單,但是我對Apache的經驗不是很豐富。

您將需要Apache的重寫模塊: mod_rewrite

然后執行以下操作:

RewriteEngine on 
RewriteRule ^content/index/(.*)$ $1

這是mod_rewrite的官方文檔: 單擊

我猜您已經對URL制定了一些重寫規則,例如http://www.example.net/content/index/mission

您需要找到這些規則並添加一個新規則,該規則使用類似的結構,但是對內容/索引部分進行“硬編碼”,例如,假設現有規則為

RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1&param2=$2 [L,qsa]

您想制定一個新規則來提取/ mission並以類似的方式重寫它,但是要現有規則生效之前 ,例如

RewriteRule ^mission$ /content.php?param1=index&param2=mission [L,qsa]
RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1&param2=$2 [L,qsa]

這些僅是示例-實際上將取決於您現有的規則。

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/removedstring/
RewriteRule ^removedstring/(.*)$ https://www.domain.eu/$1 [L,NC,R=301]
  # Get rid of index.php
  RewriteCond %{REQUEST_URI} /index\.php
  RewriteRule (.*) index.php?rewrite=2 [L,QSA]

  # Rewrite all directory-looking urls
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*) index.php?rewrite=1 [L,QSA]

或者只是根據您要刪除的擴展名添加index.html,而Apache則需要MOD_REWRITE才能起作用。 謝謝。

我還寫了一個腳本,您將它放在服務器上,然后通過Internet瀏覽器瀏覽到該腳本,它可以確認服務器上是否有mod_rewrite。 只要啟用了mod_rewrite,我向您展示的方法就可以100%工作

暫無
暫無

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

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