简体   繁体   English

htaccess动态网址重写和301重定向

[英]htaccess dynamic url rewrite and 301 redirect

I have a website with a dynamic main page, in which only one image changes, the url looks like this: 我有一个带有动态主页的网站,其中只有一个图像更改,URL如下所示:

http://mysite.com/?v=imagename.jpg http://mysite.com/?v=imagename.jpg

i am trying to find a rule to redirect all these url's to: 我试图找到一条规则来将所有这些URL重定向到:

http://mysite.com/imagename http://mysite.com/imagename

I have tried: 我努力了:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)v=([^&]+)&?(.*)?$
RewriteRule ^index\.php$ http://mysite.com/3?%1%4 [L,R=301]

and: 和:

RewriteRule ^index.php?v=([a-zA-Z]+)/([0-9]+)$ http://mysite.com/$1

and countless other variations, but nothing seems to work .. what am i doing wrong? 和无数其他变体,但似乎无济于事..我在做什么错?

To redirect a browser accessing the URL with the query string to the URL without, you need to do this: 要将访问带有查询字符串的URL的浏览器重定向到没有URL的URL,您需要这样做:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?v=([^&\ ]+?).jpg&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]

Then after the browser gets the redirect, it'll try to request the URL without the query string. 然后,浏览器获得重定向后,它将尝试请求不带查询字符串的URL。 If there isn't actually any content at /imagename then you'll need to rewrite them back to the query string'ed URI: 如果/imagename实际上没有任何内容,则需要将它们改写查询字符串的URI:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /?v=$1.jpg [L,QSA,NC]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM