如何使用.htaccess重定向重写规则链接

时间:2017-12-31 16:19:32

标签: php apache .htaccess mod-rewrite

如何使用.htaccess创建重写规则以获得漂亮的链接。例如,我有这样的链接:

http://localhost/piratefiles/view.php?idp=2&cat=Animation&pst=In-this-Corner-of-the-World-(Kono-Sekai-no-Katasumi-ni)/

我想将此网址设为这样,我的链接中不需要():等。

http://localhost/piratefiles/view/2/in-this-corner-of-the-world-kono-sekai-no-katsumi-ni

或只是

http://localhost/piratefiles/view/2/

我的代码

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} "^([^/]*?)(?:/|%20)+([^/]+?(?:/|%20)+.*)$"
RewriteRule ^ %{REQUEST_URI}?%1-%2 [N,NE]

RewriteCond %{QUERY_STRING} "^([^/]*?)(?:/|%20)+([^/]+?)/?$"
RewriteRule ^ %{REQUEST_URI}?%1-%2/ [L,R=302,NE]

1 个答案:

答案 0 :(得分:0)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/$    index.php    [L]

#Your section
RewriteRule ^view/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    view.php?idp=$1&cat=$2&pst=$3   [NC,L]

ErrorDocument 404 http://www.akshadainfosystem.com/

RewriteRule ^([^\.]+)$ $1.php [NC,L]

将此代码放入.htaccess文件并清除所有Cookie