簡體   English   中英

使用htaccess重寫規則從url中刪除不需要的字符

[英]remove unwanted characters from url using htaccess rewrite rule

在我最近的項目中,重寫規則工作正常,但問題是我無法從網址中刪除不需要的字符。 以下是htaccess代碼:-

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^(www\.)?domain.com\.com$ [NC]

RewriteCond %{THE_REQUEST} /find\.php\?source=([^\s&]+)&destination=([^\s&]+) [NC]

RewriteRule ^ %1-to-%2.html? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\s]+)-to-([^\s]+)?.html$ find.php?source=$1&destination=$2 [L,QSA]

輸入為find.php?source =印度北方邦諾伊達&目的地=印度哈里亞納邦古爾岡

輸出為: http : //domain.com/Noida%2C+Uttar+Pradesh%2C+India-to-Gurgaon%2C+Haryana%2C+India.html

我只想從網址中刪除%2C和+,並希望將其替換為-所以輸出網址將是:-

http://domain.com/Noida-Uttar-Pradesh-India-to-Gurgaon-Haryana-India.html   

您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代碼:

RewriteEngine On
RewriteBase /

# replace all comma by underscore
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:,|%2C)+(.+?)\sHTTP [NC]
RewriteRule ^ %1_%2 [L,NE,R=302]

# replace all space by hyphen
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ %1-%2 [L,NE,R=302]

# redirect long URL to a pretty one
RewriteCond %{THE_REQUEST} /find\.php\?source=([^\s&]+)&destination=([^\s&]+) [NC]
RewriteRule ^ %1-to-%2.html? [R=302,L,NE]

# route pretty URL an actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\s]+)-to-([^\s]+)?.html$ find.php?source=$1&destination=$2 [L,QSA]

暫無
暫無

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

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