[英]How to rewriterule URL using .htaccess
如何重新编写这样的链接
http://stanime.pe.hu/content.php?idf=15&link=Katsugeki--Touken+Ranbu
到http://stanime.pe.hu/15/Katsugeki--Touken+Ranbu
我使用 Urlencode 和 urldecode 对我的 url 进行编码和解码
在数据库整理中使用 latin1_swedish_ci
因此, space
将替换为+
和/
替换为%2f
和更多...
这是我的 .htaccess 代码
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
文件中添加此指令:
AllowEncodedSlashes On
这将允许编码的斜杠字符,即 URL 中的%2F
。
您可以在站点根目录 .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]
您必须添加如下字符规则。 因为您的数据不仅是您在此处定义的 url 中的数字,而且是字符形式。
RewriteRule ^content/([A-Za-z0-9-_]+)/?$ content.php?link=$1 [NC,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.