.htaccess Rediret动态网址到自定义网址

时间:2016-07-30 07:42:10

标签: .htaccess mod-rewrite url-rewriting friendly-url

我有这样的网址结构

localhost/sumar/gallery.php?gen=slug-here

我想要清理例如:localhost/sumar/gallery/slug-here

因此我需要针对此情况重写规则,并阻止任何人通过自动将其重定向到http://localhost/sumar/gallery.php?gen=slug-here来直接点击基本网址localhost/sumar/gallery/slug-here

到目前为止,我尝试过这种方法:

//that works

RewriteRule ^Gallery/([A-Za-z0-9-_]+)/?$ gallery.php?gen=$1 

//below one does not seems to work

RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]    
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]

1 个答案:

答案 0 :(得分:1)

您需要对原始请求采取行动:

RewriteEngine On

RewriteRule ^(sumar)/Gallery/([\w-]+)/?$ /$1/gallery.php?gen=$1

RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]

PS:字符集[A-Za-z0-9_]可以用\w表示。