简体   繁体   中英

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:

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

i am trying to find a rule to redirect all these url's to:

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:

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. If there isn't actually any content at /imagename then you'll need to rewrite them back to the query string'ed URI:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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