How to rewriterule URL using .htaccess

拟墨画扇 提交于 2019-12-18 07:15:00

问题


how to rewriterule for link like this

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

to http://stanime.pe.hu/15/Katsugeki--Touken+Ranbu

im encode and decode my url using Urlencode and urldecode

in database Collation im using latin1_swedish_ci

so, space will replace with + and / replace with %2f and more...

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]

回答1:


First you need to add this directive in your vhost or http.conf file:

AllowEncodedSlashes On

This will allow encoded slash character i.e. %2F in URLs.

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]



回答2:


you have to add character rules like below. Because your data is in character form not only number in url that you have defined here.

  RewriteRule    ^content/([A-Za-z0-9-_]+)/?$    content.php?link=$1 [NC,L]  


来源:https://stackoverflow.com/questions/44758614/how-to-rewriterule-url-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!