I need a htaccess code to redirect this url
domain.com/download.php?server=1&file=/movies/33.jpeg
to
domain.com/movies/33.jpeg
IF url contains JPEG
how it is possible?
To rewrite from an URL to a script with query string, you can use this simple RewriteRule
RewriteRule ^movies/.*?\.jpeg$ ^download.php?server=1&file=%{REQUEST_URI} [L]
When a user enters http://domain.com/movies/33.jpeg, the server will serve /download.php?server=1&file=/movies/33.jpeg
To redirect from URL with query string, you must first capture the parts with RewriteCond and then use these parts in a RewriteRule
RewriteCond %{QUERY_STRING} file=(.*?\.jpeg)
RewriteRule ^download.php$ /movies/%1? [R,L]
When a user enters http://domain.com/download.php?server=1&file=/movies/33.jpeg, the server will redirect the browser to http://domain.com/movies/33.jpeg
Related
Hi all I need to redirect based on query string though htaccess in Apache.
I want that all the queries that contain jjj redirect to my homepage
I used the code below but its not working could you please help me¿
Thanks in advance,
Mauri
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://www.eventosbarcelona.com [R=301,L]
Your rule redirects example.com/?jjj to example.com/?jjj I guess, You are getting redirect loop error as both urls are same. By default, mod-rewrite appends old query strings to the destination url. You need to use a ? at the end of the Rewrite destination url to discard query string.
Try this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://example.com? [L,R]
This will redirect /?jjj to http://example.com/ .
Because of the way that my file structure was set up, I have folders that I don't want showing in the URL.
I want to redirect this URL:
https://portal.domain.com/portal.domain.com/portal/test.php
To this:
https://portal.domain.com/test
I have tried this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^portal.domain.com
RewriteRule ^(.*)$ https://portal.domain.com/portal.domain.com/portal/$1 [L,NC]
But I get a 'too many redirects' message.
the rewrite rule: RewriteCond %{HTTP_HOST} ^portal.domain.com
is too generic, then:
1) the user open https://portal.domain.com/portal.domain.com/portal/test.php
2) the rewrite rule match and redirect to https://portal.domain.com/test.php
3) but when the user request the page https://portal.domain.com/test.php
the rewrite rule match again and will redirect the user to https://portal.domain.com/test.php
4,5,6,7,8,9.....) then the user opens the same page and the rewrite rlue match again and again in an infinite loop.
Solution: the condition RewriteCond %{HTTP_HOST} ^portal.domain.com must be less generic and not math the redirect location.
On my current website I use parameters in querystrings to select the content shown to the user (e.g. www.domain.tld/index.php?site=linklist&page=2). I want to switch to wordpress and have all old links redirected to the new permalinks of wordpress.
When having site=linklist in the query string the user should be redirected to www.domain.tld/my-new-linklist . If the querystring contains site=about the user should be redirected to www.domain.tld/about-me ,...
How can I realize this using htaccess?
In the htaccess file in your document root, try adding these above any of your wordpress rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|.*)site=linklist&?($|.*)
RewriteRule ^/?index.php$ /my-new-linklist?%1%2 [L,R=301]
RewriteCond %{QUERY_STRING} (^|.*)site=about&?($|.*)
RewriteRule ^/?index.php$ /about-me?%1%2 [L,R=301]
etc.
Any other query string gets passed along, but the site=something is stripped out.
I successfully changed my URLs from ugly ones with several parameters in the querystring to clean looking ones with the help of mod rewrite. However, there are many url's for my site. Rather than go back and edit the href attribute on each and every one of my anchor tags, I tried to write a redirect function in the .htaccess file that automatically redirects the old url to the new one.
In my .htaccess file, I have the following:
RewriteEngine On
Redirect teams.php?league=$1&team=$2&year=$3&tab=$4 teams/(.*)/(.*)/(.*)/(.*)
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]
No luck though... any thoughts?
Thanks
You need to do a check that the old URL with the php in it is actually being requested by matching against %{THE_REQUEST}, otherwise it'll redirect loop forever (e.g. user goes to team.php, serve redirects to teams, browser requests teams, server rewrites as teams.php, server sees "teams.php" and redirects to teams, browser requests teams, server rewrites as teams.php, etc. etc.)
RewriteEngine On
# redirect when the user actually requests for teams.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /teams\.php\?league=([^&]+)&team=([^&]+)&year=([^&]+)&tab=([^\ ]+)
RewriteRule ^teams\.php$ /teams/%1/%2/%3/%4? [R=301,L]
# internally rewrite any /teams/ URI
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]
I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113¶m=value will not be redirected because query string is group=113¶m=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]