使用 HTaccess 重写规则从 URL 中删除不需要的字符


remove unwanted characters from url using htaccess rewrite rule

在我最近的项目中,重写规则工作正常,但问题是我无法从 URL 中删除不需要的字符。以下是访问代码:-

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=Noida, Uttar Pradesh, India&destination=Gurgaon, Haryana, India

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

我只想从 url 中删除 %2C 和 +,并想将它们替换为此 - 以便输出 url 将是:-

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]