使用htaccess重写规则从url中删除不需要的字符 [英] remove unwanted characters from url using htaccess rewrite rule

查看:135
本文介绍了使用htaccess重写规则从url中删除不需要的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in my recent project, rewrite rule is working fine but problem is that i am not able to remove unwanted characters from the url. Below is the htaccess code :-

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,印度北方邦& destination =古尔冈,印度哈里亚纳邦

input is 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

我只想从网址中删除%2C和+,并希望将其替换为该网址-因此输出网址将为:-

I just want to remove %2C and + from url and want to replace them to this - so that output url will be :-

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


推荐答案

您可以在 DOCUMENT_ROOT / .htaccess 文件中使用此代码:

You can use this code in your DOCUMENT_ROOT/.htaccess file:

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]

这篇关于使用htaccess重写规则从url中删除不需要的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆