如何使用 .htaccess 重写 URL [英] How to rewriterule URL using .htaccess

查看:27
本文介绍了如何使用 .htaccess 重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重写这样的链接

http://stanime.pe.hu/content.php?idf=15&link=Katsugeki--Touken+Ranbu

http://stanime.pe.hu/15/Katsugeki--刀剑+乱舞

我使用 Urlencode 和 urldecode 对我的 url 进行编码和解码

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...

这是我的 .htaccess 代码

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屋!

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