Redirect pattern file avoiding recursion loop - .htaccess

I have a downloadable file with the filename containing a version number, let's say download-3-0.dat, would be the current one.
I want to redirect external references to previous versions of the file to the current one so download-2-5.dat would redirect to download-3-0.dat
This seems a partial solution:
RewriteRule .* - [E=CURRENT:3-0]
RewriteRule ^download(.*).dat$ http://site.com/download-%[ENV:CURRENT].dat [L, R=301]
But the problem is how do you add an exception to the current version so it does not enter a recursion loop (the last rewrite rule would redirect to itsef if the requested file is download-3-0.dat)

You can replace all of your code with this line:
RewriteRule ^download-(?!3-0\.dat$) /download-3-0.dat [L,R=301,NC]
EDIT: Based on your comment below :-
Hmm this turned out to be way more tricky than I anticipated at start.
Try this code:
# set your current version here
SetEnvIf Request_URI "^" CURRENT=3-0
RewriteCond %{ENV:CURRENT}:%{REQUEST_URI} !^([^:]+):/download-\1\.dat$ [NC]
RewriteRule ^download-[^.]+\.dat$ /download-%{ENV:CURRENT}.dat [L,R=301,NC]
PS: Since we cannot use % variables on RHS of RewriteCond as back-reference, I am using special regex back-reference variable \1 in the RewriteCond here.

I think you just need a RewriteCond that confirms it isn't the current version:
RewriteRule .* - [E=CURRENT:3-0]
RewriteCond %{REQUEST_URI} !^download-3-0.dat
RewriteRule ^download(.*).dat$ http://site.com/download-%{ENV:CURRENT}.dat [L, R=301]
You may have to tweak the pattern in the RewriteCond, but this should get you headed in the right direction.

Related

How does rewrite URL and 301 redirection work together

I've got:
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
Which points page/([^/\.]+)/? to index.php?page=$1.
When I go to page/([^/\.]+)/, I don't want to see index.php (which is achieved from the above)
For the reverse, when I go to index.php, I want to see a visible 301 redirection to page/([^/\.]+)/.
How do I do this without causing an infinite loop... or do I only rely on canonical tags?
Update:
I got it happening in one direction (new to old), but not the other (Old to new)
RewriteRule ^([^/\.]+)-yum/?$ yum/?x=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)-yum/?$ yum/?x=$2&y=$1 [L]
You need to do a check against the %{THE_REQUEST} variable so that you are rewriting only when the actual request is for the index.php file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&]+)([^\ ]*)
RewriteRule ^/?index\.php$ /page/%1/?%2 [L,R=301]
So if a browser requests http://yourdomain.com/index.php?page=qwe&someother=var
It should get redirected to: http://yourdomain.com/page/qwe/?someother=var
Full solution here:
RewriteCond %{QUERY_STRING} Variable=([A-Za-z]+)$
RewriteRule OldURL/$ /NewURL/%1? [L,R=301]
RewriteRule ^NewUrl/([^/\.]+)$ MappedNewLocationFolder/?Variable=$1 [L]
1st Line: Check (the name and number of) parameters are as expected
2nd Line: Check base path of parameter(s), which is OldURL is correct and 301 to NewUrl
3rd Line: Map NewUrl to actual location where file sits (not shown in browser)
(The question mark right at the end of 2nd line strips out the variables from appending to the NewUrl)

Rewrite htaccess old oscommerce links

I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.
The link looks like this:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285
This rewrite works for the above link:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
But will not work for:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129
My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.
I think you can achieve this by not specifying the closing delimiter ($)
Give this a try:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
By not putting the $ at the end of the regex string you are basically saying: match any string that starts with ..., no matter what comes after
Hope this helps :)
This should do the job just fine:
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^product_info\.php$ http://www.domain.com/apple/air.html? [R=301,L]
There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info\.php$ -- this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.
This is enough ^cPath=3_72&products_id=129 -- it tells "When query strings STARTS with ...". No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).
This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.

.htaccess 301 redirect problem

i try to redirect url's like:
example.com/video/1640/video-name
to
example.com/video/1640/video-name/
i've tried with:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
but it is not working
my currently htaccess file has only the first line:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
and videos only loads at
example.com/video/1640/video-name/
url type
i want to redirect the non-backslash url type
example.com/video/1640/video-name
to the correct one (the one with the backslash)
How can i do this?
Your second rule should be RewriteRule ^video/([^/]*)/([^/]*)$ /video/$1/$2/ [R=301,L]
Or you could forgo the redirect totally, and just say RewriteRule ^video/([^/]*)/([^/]*)/?$ video.php?id=$1&title=$2 [L] which will allow both to view your video.
Update FallingBullets is right (see the comments on this answer), his answer better suites the OP's problem, so please ignore this answer (I am leaving it for reference, though).
Maybe you simply have to prefix your pattern with a /?? E. g.
RewriteRule ^/?video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^/?video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
# ^ these ones
instead of
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
since you are anchoring the pattern at the beginning of the path (using ^).

mod_rewrite regex (too many redirects)

I am using mod_rewrite, to convert subdomains into directory urls. (solution from here). When I explicity write a rule for one subdomain, it works perfectly:
RewriteCond %{HTTP_HOST} ^[www\.]*sub-domain-name.domain-name.com [NC]
RewriteCond %{REQUEST_URI} !^/sub-domain-directory/.*
RewriteRule ^(.*) /sub-domain-directory/$1 [L]
However, if I try to match all subdomains, it results in 500 internal error (log says too many redirects). The code is:
RewriteCond %{HTTP_HOST} ^[www\.]*([a-z0-9-]+).domain-name.com [NC]
RewriteCond %{REQUEST_URI} !^/%1/.*
RewriteRule ^(.*) /%1/$1 [L]
Can anyone suggest what went wrong and how to fix it?
Your second RewriteCond will never return false, because you can't use backreferences within your test clauses (they're compiled during parsing, making this impossible since no variable expansion will take place). You're actually testing for paths beginning with the literal text /%1/, which isn't what you wanted. Given that you're operating in a per-directory context, the rule set will end up being applied again, resulting in a transformation like the following:
path -> sub/path
sub/path -> sub/sub/path
sub/sub/path -> sub/sub/sub/path
...
This goes on for about ten iterations before the server gets upset and throws a 500 error. There are a few different ways to fix this, but I'm going to chose one that most closely resembles the approach you were trying to take. I'd also modify that first RewriteCond, since the regular expression is a bit flawed:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteCond %1 !=www
RewriteCond %1#%{REQUEST_URI} !^([^#]+)#/\1/
RewriteRule .* /%1/$0 [L]
First, it checks the HTTP_HOST value and captures the subdomain, whatever it might be. Then, assuming you don't want this transformation to take place in the case of www, it makes sure that the capture does not match that. After that, it uses the regular expression's own internal backreferences to see if the REQUEST_URI begins with the subdomain value. If it doesn't, it prepends the subdomain as a directory, like you have now.
The potential problem with this approach is that it won't work correctly if you access a path beginning with the same name as the subdomain the request is sent to, like sub.example.com/sub/. An alternative is to check the REDIRECT_STATUS environment variable to see if an internal redirect has already been performed (that is, this prepending step has already occurred):
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteCond %1 !=www
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule .* /%1/$0 [L]

.htaccess questions

Say I have the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image01.gif$ http://www.domain.tld/images/partner/image01.gif [NC,QSA]
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image02.gif$ http://www.domain.tld/images/partner/image02.gif [NC,QSA]
What do NC and QSA mean?
Also, instead of repeating the same RewriteCond twice is there to use it just once and have it apply to both RewriteRules?
Finally, if the above .htaccess is located at http://www.domain.tld/images/ why doesn't a RewriteRule like this work?:
RewriteRule ^image02.gif$ /images/partner/image02.gif [NC,QSA]
Or maybe this?:
RewriteRule ^image02.gif$ partner/image02.gif [NC,QSA]
The square bracket options are documented in the RewriteRule manual page:
'nocase|NC' (no case):
This makes the Pattern case-insensitive, ignoring difference
between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
'qsappend|QSA' (query string append):
This flag forces the rewrite engine to append a query string part
of the substitution string to the
existing string, instead of replacing
it. Use this when you want to add more
data to the query string via a rewrite
rule.
As far as I know, the RewriteCond directives affect the RewriteRule they precede. If you were setting rules in the main confing file you could write the common directives in a file and include it several times but that's not an option in .htaccess files, sorry.
Your directive works for me, although you probably mean this:
RewriteRule ^image02\.gif$ /images/partner/image02.gif [NC,QSA]
How are you testing it exactly?
NC is for No Case, meaning it can be upper or lower case and it will take you to the same page.
QSA is for query string append. Not really sure on this one, however a quick search http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html sheds a bit more light on this one.

Resources