如何使用.htaccess重写URL [英] How to rewriterule URL using .htaccess
本文介绍了如何使用.htaccess重写URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为这样的链接改写
http://stanime.pe.hu/content.php?idf=15&link=Katsugeki--Touken+Ranbu
到http://stanime.pe.hu/15/Katsugeki--Touken+Ranbu
im使用Urlencode和urldecode对我的网址进行编码和解码
im encode and decode my url using Urlencode and urldecode
在数据库排序规则中使用latin1_swedish_ci
in database Collation im using latin1_swedish_ci
因此,space
将替换为+
,而/
将替换为%2f
等.
so, space
will replace with +
and /
replace with %2f
and more...
这是我的.htacces代码
this my .htacces code
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+content\.php\?link=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ content.php?link=$1 [B,L,QSA]
推荐答案
首先,您需要将此指令添加到您的vhost或http.conf
文件中:
First you need to add this directive in your vhost or http.conf
file:
AllowEncodedSlashes On
这将允许在URL中使用编码的斜杠字符,即%2F
.
This will allow encoded slash character i.e. %2F
in URLs.
您可以在网站根.htaccess中使用以下规则:
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+content\.php\?idf=([^\s&]+)&link=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/(.+)$ content.php?idf=$1&link=$2 [B,L,QSA]
这篇关于如何使用.htaccess重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文